-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.java
More file actions
119 lines (107 loc) · 3.95 KB
/
Dijkstra.java
File metadata and controls
119 lines (107 loc) · 3.95 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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Dijkstra {
public static class Graph {
int adjacency[][];
int size;
public Graph(int size) {
this.size = size;
adjacency = new int[size][size];
}
public void addEdge(int first, int second,int weight) {
if(adjacency[first][second] == 0 || adjacency[first][second] > weight){
adjacency[first][second] = weight;
adjacency[second][first] = weight;
}
}
public int[] shortestReach(int startId) { // 0 indexed
int[] distances = new int[size];
for(int i=0;i<size;i++)
distances[i] = 1000001;
distances[startId] = 0;
PriorityQueue<Node> queue = new PriorityQueue<Node>();
queue.add(new Node(startId,0));
for(int i=0;i<size;i++){
if(i != startId)
queue.add(new Node(i,1000001));
}
while(!queue.isEmpty()){
Node current = queue.remove();
for(int i=0;i<size;i++){
if(adjacency[current.name][i] != 0){
if(distances[i] > current.priority+adjacency[current.name][i]){
queue.remove(new Node(i,distances[i]));
distances[i] = current.priority+adjacency[current.name][i];
queue.add(new Node(i,distances[i]));
}
}
}
}
return distances;
}
public void printGraph(){
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
System.out.print(adjacency[i][j]+" ");
}
System.out.println();
}
}
}
public static void main(String[] args) throws IOException{
//Scanner scanner = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int queries = Integer.parseInt(br.readLine());//scanner.nextInt();
for (int t = 0; t < queries; t++) {
String[] ip1 = br.readLine().split(" ");
// Create a graph of size n where each edge weight is 6:
Graph graph = new Graph(Integer.parseInt(ip1[0]));
int m = Integer.parseInt(ip1[1]);
// read and set edges
for (int i = 0; i < m; i++) {
String[] ip2 = br.readLine().split(" ");
int u = Integer.parseInt(ip2[0]) - 1;
int v = Integer.parseInt(ip2[1]) - 1;
int weight = Integer.parseInt(ip2[2]);
// add each edge to the graph
graph.addEdge(u, v, weight);
}
// Find shortest reach from node s
int startId = Integer.parseInt(br.readLine()) - 1;
int[] distances = graph.shortestReach(startId);
for (int i = 0; i < distances.length; i++) {
if (i != startId && distances[i] != 1000001) {
System.out.print(distances[i]);
System.out.print(" ");
}
else if(distances[i] == 1000001){
System.out.print("-1");
System.out.print(" ");
}
}
System.out.println();
//graph.printGraph();
}
//scanner.close();
}
}
class Node implements Comparable{
int name;
int priority;
public Node(int name,int priority){
this.name = name;
this.priority = priority;
}
public int compareTo(Object o) {
Node n = (Node)o;
if(this.priority > n.priority)
return 1;
else if(this.priority < n.priority)
return -1;
else
return 0;
}
}