-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstraForParallelEdges.java
More file actions
228 lines (165 loc) · 4.38 KB
/
Copy pathDijkstraForParallelEdges.java
File metadata and controls
228 lines (165 loc) · 4.38 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
//import java.util.Scanner;
public class Main{
private static int n , e;
static int mat[][];
private static int t;
private static int clock = 0;
private static ArrayList<Integer>[] adj1 , adj2 , cost;
private static ArrayList<Integer> visited , pre , post , prev , dist;
// private static Scanner sc = new Scanner(System.in);
private static PriorityQueue<Integer> q;
private static BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
public static void relax(int top){
//RELAX ALL EDGES OF THIS NEW COVERED NODE.
for(int i=0 ; i<adj1[top].size() ; i++){
int w = adj1[top].get(i);
if(q.contains(w) && dist.get(w)> dist.get(top) + cost[top].get(i) && dist.get(top)!= Integer.MAX_VALUE){
dist.set(w , dist.get(top) + cost[top].get(i));
q.remove(w);
q.add(w);
}
}
}
public static void dj(int start){
int mystart = start-1;
dist.set(mystart , 0);
//visited.set(mystart , 1);
q = new PriorityQueue<Integer>(10 , new Comparator<Integer>(){
public int compare(Integer a , Integer b){
if(dist.get(a)>dist.get(b)){
return 1;
}
if(dist.get(a)<dist.get(b)){
return -1;
}
return 0;
}
});
//Q IS THE UNCOVERED REGION.
for(int i=0 ; i<n ; i++)
q.add(i);
while(!q.isEmpty()){
relax(q.poll());
}
}
public static void main(String[] args) throws NumberFormatException, IOException{
t = Integer.parseInt(br.readLine());
while(t-- > 0 ){
GetInp();
dj(Integer.parseInt(br.readLine()));
for(int x : dist)
if(x!=0)
System.out.print(x==Integer.MAX_VALUE ? -1 + " " : x + " " );
System.out.println();
}
}
public static void BFS(int start){
int mystart = start-1;
dist.set(mystart , 0);
visited.set(mystart , 1);
Queue<Integer> q = new LinkedList();
q.add(mystart);
while(!q.isEmpty()){
int top = q.poll();
for(int w : adj1[top]){
if(visited.get(w)==0){
visited.set(w, 1);
q.add(w);
dist.set(w, dist.get(top)+1);
}
}
}
}
public static void DFS(){
for(int i=0 ; i<n ; i++)
if(visited.get(i)==0)
Explore(i , adj1);
}
public static void Explore(int v , ArrayList<Integer>[] adj){
pre.set(v , clock++);
visited.set(v, 1);
for(int w : adj1[v]){
if(visited.get(w)==0){
visited.set(w , 1);
Explore(w , adj1);
}
}
post.set(v , clock++);
}
public static void GetInp() throws NumberFormatException, IOException{
String sin = (br.readLine());
String num [] = sin.split(" ");
n = Integer.parseInt(num[0]);
e = Integer.parseInt(num[1]);
mat = new int[n][n];
for(int i=0 ; i<n ; i++)
for(int j=0 ; j<n ; j++)
mat[i][j]=Integer.MAX_VALUE;
//GET NO. OF NODES AND EDGES.
//n = sc.nextInt();
//e = sc.nextInt();
//INITIALIZE CONTAINERS.
visited = new ArrayList<Integer>();
pre = new ArrayList<Integer>();
post = new ArrayList<Integer>();
dist = new ArrayList<Integer>();
prev = new ArrayList<Integer>();
//..
adj1 = (ArrayList<Integer>[]) new ArrayList[n];
adj2 = (ArrayList<Integer>[]) new ArrayList[n];
cost = (ArrayList<Integer>[]) new ArrayList[n];
//..
for(int i=0 ; i<n ; i++){
adj1[i] = new ArrayList<>();
adj2[i] = new ArrayList<>();
cost[i] = new ArrayList<>();
dist.add(Integer.MAX_VALUE);
visited.add(0);
pre.add(0);
post.add(0);
prev.add(-1);
}
//GET VALUES
int a , b, c;
for(int z=0 ; z<e ; z++){
String edgein = br.readLine();
String[] ea = edgein.split(" ");
a = Integer.parseInt(ea[0]);
b = Integer.parseInt(ea[1]);
c = Integer.parseInt(ea[2]);
if(mat[a-1][b-1]>c || mat[b-1][a-1]>c) {
mat[a-1][b-1]=c;
mat[b-1][a-1]=c;
}
// UNDIRECTED GRAPH TAKEN.
// for(int i=0 ; i<adj1[a-1].size() ; i++) {
// if(adj1[a-1].get(i)== b) {
// if(cost[a-1].get(i)>c) {
// cost[a-1].set(i, c);
// cost[b-1].set(i, c);
// continue;
// }
// }
// }
}
for(int i=0 ; i<n ; i++)
for(int j=0 ; j<n ; j++) {
int x = mat[i][j];
if(x!=Integer.MAX_VALUE) {
adj1[i].add(j);
adj1[j].add(i);
cost[i].add(x);
cost[j].add(x);
}
}
//System.out.println("Input successfully read!...");
}
}