-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra_Example.java
More file actions
155 lines (142 loc) · 4.67 KB
/
Dijkstra_Example.java
File metadata and controls
155 lines (142 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Dijkstra_Example {
static class Graph{
class Node{
int d;
int w;
public Node(int d, int w) {
this.d = d;
this.w = w;
}
@Override
public String toString() {
return "{d=" + d + ", w=" + w + '}';
}
}
int n;
ArrayList<Node>[] adjMatrix;
public Graph(int n) {
this.n = n;
adjMatrix = new ArrayList[n*2];
for(int i=0;i<n*2;i++)
{
adjMatrix[i] = new ArrayList<>();
}
}
public void addEdge(int s, int d, int w) {
s--;
d--;
Node x = new Node(d, w);
adjMatrix[s].add(x);
}
int minVertex(boolean [] min, int [] k){
int leastKey = Integer.MAX_VALUE;
int vertex = -1;
for (int i = 0; i <n ; i++) {
if(min[i]==false && leastKey>=k[i]){
leastKey = k[i];
vertex = i;
}
}
return vertex;
}
int getTwoCount(ArrayList<Integer> arr)
{
int countTwo = 0;
for(int i=0;i<arr.size();i++)
{
if (arr.get(i) == 2)
countTwo++;
}
return countTwo;
}
void enhance()
{
for (int i=0;i<n;i++)
{
for (int j=0;j<adjMatrix[i].size();j++)
{
adjMatrix[i+n].add(new Node(adjMatrix[i].get(j).d+n, adjMatrix[i].get(j).w));
}
}
for (int i=0;i<n;i++)
{
int x = adjMatrix[i].size();
for (int j=0;j<x;j++)
{
if (adjMatrix[i].get(j).w == 0)
{
adjMatrix[i].add(new Node((adjMatrix[i].get(j).d+n), 0));
}
}
}
n=n*2;
}
public void dijkstra(int src){
/*for (int i = 0; i <n ; i++) {
System.out.print(i + " -> ");
for (int v = 0; v <adjMatrix[i].size() ; v++) {
System.out.print(adjMatrix[i].get(v).toString() + ", ");
}
System.out.println();
}*/
boolean[] check = new boolean[n];
int [] d = new int[n];
ArrayList<Integer>[]edges;
edges = new ArrayList[n];
for (int i = 0; i <n ; i++) {
d[i] = Integer.MAX_VALUE;
edges[i] = new ArrayList<>();
}
d[src] = 0;
for (int i = 0; i <n ; i++) {
int u = minVertex(check, d);
check[u] = true;
/*for (int xx = 0; xx <n ; xx++) {
System.out.print("(" + d[xx] + "+" + check[xx] + "), ");
}
System.out.println();*/
for (int v = 0; v <adjMatrix[u].size() ; v++) {
int v1 = adjMatrix[u].get(v).d;
if(check[v1]==false){
int updatedKey = d[u]+adjMatrix[u].get(v).w;
if(updatedKey<=d[v1])
{
d[v1] = updatedKey;
}
}
}
}
/*for (int i = 0; i <n ; i++) {
System.out.print("(" + d[i] + "+" + check[i] + "), ");
}*/
if (d[n-1] == Integer.MAX_VALUE)
System.out.println(-1);
else
System.out.println(d[n-1]);
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String []splitted = line.split(" ");
int n = Integer.parseInt(splitted[0]);
int m = Integer.parseInt(splitted[1]);
Graph graph = new Graph(n);
for (int x = 0;x<m;x++)
{
line = scanner.nextLine();
splitted = line.split(" ");
graph.addEdge(Integer.parseInt(splitted[0]), Integer.parseInt(splitted[1]), Integer.parseInt(splitted[2])-1);
}
int sourceVertex = 0;
graph.enhance();
graph.dijkstra(sourceVertex);
}
}