-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.java
More file actions
69 lines (57 loc) · 1.67 KB
/
Copy pathdijkstra.java
File metadata and controls
69 lines (57 loc) · 1.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
package templates;
import java.util.*;
import java.io.*;
public class dijkstra {
static long inf = (long) 4e18;
static long[] dists;
static int[] par;
static boolean[] visited;
static ArrayList<ArrayList<long[]>> adj;/* adjacency list with weight */
// initialisation
public static void init(int n) {
dists = new long[n];
par = new int[n];
visited = new boolean[n];
adj = new ArrayList<>();
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
}
// addedge
public static void addedge(int a, int b, long wt) {
adj.get(a).add(new long[] { wt, b });
adj.get(b).add(new long[] { wt, a });
}
// dijkstra
public static void run(int src) {
Arrays.fill(dists, inf);
Arrays.fill(visited, false);
Arrays.fill(par, -1);
PriorityQueue<long[]> pq = new PriorityQueue<>((a, b) -> {
if (a[0] > b[0])
return 1;
else if (a[0] == b[0])
return 0;
else
return -1;
});/* (dist,vertex) */
dists[src] = 0;
pq.add(new long[] { 0, src });
while (!pq.isEmpty()) {
long[] foc = pq.poll();
int v = (int) foc[1];
if (visited[v])
continue;
visited[v] = true;
for (long[] x : adj.get(v)) {
long d = dists[v] + x[0];
int u = (int) x[1];
if (d < dists[u]) {
dists[u] = d;
par[u] = v;
pq.add(new long[] { d, u });
}
}
}
}
}