-
Notifications
You must be signed in to change notification settings - Fork 7
/
Dijkstras.java
69 lines (51 loc) · 1.52 KB
/
Dijkstras.java
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
// Brett Fazio, Dijkstra's Shortest Path, N^2 time
// Find all the nodes distances from the source
import java.util.*;
public class Dijkstras {
static boolean[] shortSet;
static int[][] weight;
static int[] distance;
static int current, nodes, edges;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
nodes = sc.nextInt();
edges = sc.nextInt();
int start = sc.nextInt()-1;
weight = new int[nodes][nodes]; shortSet = new boolean[nodes];
distance = new int[nodes];
Arrays.fill(distance, Integer.MAX_VALUE);
distance[start] = 0;
for (int i = 0; i < nodes; i++)
for (int j = 0; j < nodes; j++)
weight[i][j] = Integer.MAX_VALUE;
for (int i = 0; i < edges; i++) {
int a = sc.nextInt()-1; int b = sc.nextInt()-1;
int w = sc.nextInt();
weight[a][b] = weight[b][a] = w;
}
current = start;
for (int i =0 ; i < nodes-1; i++) {
int u = minDistance();
shortSet[u] = true;
for (int v = 0; v < nodes; v++) {
if (!shortSet[v] && weight[u][v] != Integer.MAX_VALUE
&& distance[u]+weight[u][v] < distance[v])
distance[v] = distance[u] + weight[u][v];
}
}
// Distance will now contain the distances all the nodes have
// from the source node.
System.out.println(Arrays.toString(distance));
}
public static int minDistance() {
int min = Integer.MAX_VALUE;
int mini = -1;
for (int v = 0; v < nodes; v++) {
if (!shortSet[v] && distance[v] <= min) {
min = distance[v];
mini = v;
}
}
return mini;
}
}