-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commandos.java
80 lines (69 loc) · 2.11 KB
/
Commandos.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
/**
* http://www.lightoj.com/volume_showproblem.php?problem=1174 tag: #dijkstra #shortest-path use
* dijkstra from each vertex to S and T. get maximum of each cost of path S-> T via each vertex
* <<break down the path>>
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
class Commandos {
static void Dijkstra(ArrayList<Node>[] graph, int source, int[] distArr) {
PriorityQueue<Node> pq = new PriorityQueue<>();
distArr[source] = 0;
pq.add(new Node(source, distArr[source]));
while (!pq.isEmpty()) {
Node node = pq.poll();
int id = node.id;
int w = node.dist;
for (Node neighbour : graph[id]) {
if (w + neighbour.dist < distArr[neighbour.id]) {
distArr[neighbour.id] = w + neighbour.dist;
pq.add(new Node(neighbour.id, distArr[neighbour.id]));
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCases = sc.nextInt();
for (int t = 1; t < testCases + 1; t++) {
int n = sc.nextInt();
int r = sc.nextInt();
ArrayList<Node>[] graph = new ArrayList[n];
for (int i = 0; i < n; i++) {
graph[i] = new ArrayList<Node>();
}
for (int i = 0; i < r; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
graph[u].add(new Node(v, 1));
graph[v].add(new Node(u, 1));
}
int source = sc.nextInt();
int destination = sc.nextInt();
int[] distArrS = new int[n];
int[] distArrT = new int[n];
Arrays.fill(distArrS, n);
Arrays.fill(distArrT, n);
Dijkstra(graph, source, distArrS);
Dijkstra(graph, destination, distArrT);
int result = 0;
for (int i = 0; i < n; i++) {
result = Math.max(distArrS[i] + distArrT[i], result);
}
System.out.println("Case " + t + ": " + result);
}
}
}
class Node implements Comparable<Node> {
int id;
int dist;
Node(int _id, int _dist) {
this.id = _id;
this.dist = _dist;
}
public int compareTo(Node other) {
return this.dist - other.dist;
}
}