-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathJohnson.java
161 lines (126 loc) · 3.79 KB
/
Johnson.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
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
/*
* Johnson's algorithm solves the all-pairs shortest path (APSP) problems. It works with negative edge weights by using the Bellman-Ford algorithm to reweight the vertices.
* After reweighting the edges, the algorithm runs Djikstra's algorithm on every vertex.
*
* Time complexity: O(V^2 log V + EV)
*/
package codebook.graph.shortestpath;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Johnson {
static BufferedReader br;
static PrintWriter out;
static StringTokenizer st;
static int n, m, q;
static ArrayList<ArrayList<Edge>> adj;
static int[] h;
static int[][] dist;
static PriorityQueue<Vertex> pq;
public static void main (String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
//br = new BufferedReader(new FileReader("in.txt"));
//out = new PrintWriter(new FileWriter("out.txt"));
n = readInt();
m = readInt();
q = readInt();
adj = new ArrayList<ArrayList<Edge>>();
h = new int[n + 1];
dist = new int[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
adj.add(new ArrayList<Edge>());
h[i] = 1 << 29;
}
for (int i = 1; i <= n; i++)
adj.get(0).add(new Edge(i, 0));
for (int i = 0; i < m; i++) {
int a = readInt();
int b = readInt();
int c = readInt();
adj.get(a).add(new Edge(b, c));
}
h[0] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j <= n; j++)
for (Edge e : adj.get(j))
if (h[e.dest] > e.cost + h[j])
h[e.dest] = e.cost + h[j];
for (int i = 0; i <= n; i++)
for (Edge e : adj.get(i))
if (h[e.dest] > e.cost + h[i]) {
out.println("MIN COST CYCLE DETECTED");
out.close();
return;
}
for (int i = 1; i <= n; i++)
dist[i] = getPath(i);
for (int i = 0; i < q; i++) {
int a = readInt();
int b = readInt();
out.println(dist[a][b] - h[a] + h[b]);
}
out.close();
}
static int[] getPath (int src) {
int[] dist = new int[n + 1];
for (int i = 0; i <= n; i++)
dist[i] = 1 << 29;
dist[src] = 0;
pq = new PriorityQueue<Vertex>();
pq.offer(new Vertex(src, 0));
while (!pq.isEmpty()) {
Vertex curr = pq.poll();
for (Edge next : adj.get(curr.index)) {
if (dist[next.dest] <= curr.cost + next.cost + h[curr.index] - h[next.dest])
continue;
dist[next.dest] = curr.cost + next.cost + h[curr.index] - h[next.dest];
pq.offer(new Vertex(next.dest, dist[next.dest]));
}
}
return dist;
}
static class Vertex implements Comparable<Vertex> {
int index, cost;
Vertex (int index, int cost) {
this.index = index;
this.cost = cost;
}
@Override
public int compareTo (Vertex o) {
return cost - o.cost;
}
}
static class Edge {
int dest, cost;
Edge (int dest, int cost) {
this.dest = dest;
this.cost = cost;
}
}
static String next () throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
static long readLong () throws IOException {
return Long.parseLong(next());
}
static int readInt () throws IOException {
return Integer.parseInt(next());
}
static double readDouble () throws IOException {
return Double.parseDouble(next());
}
static char readCharacter () throws IOException {
return next().charAt(0);
}
static String readLine () throws IOException {
return br.readLine().trim();
}
}