-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1238.java
More file actions
79 lines (66 loc) · 2.35 KB
/
Copy pathJ1238.java
File metadata and controls
79 lines (66 loc) · 2.35 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
package TIL;
import java.io.*;
import java.util.*;
public class J1238 {
static int n, e, x;
static int[][] city;
static ArrayList<int[]>[] list;
static boolean[] visited;
public static void main(String[] args)throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String[] input = buffer.readLine().split(" ");
n = Integer.parseInt(input[0]);
e = Integer.parseInt(input[1]);
x = Integer.parseInt(input[2]);
list = new ArrayList[n+1];
for(int i = 0 ; i <= n ; i++){
list[i] = new ArrayList<>();
}
city = new int[n+1][n+1];
for(int i = 1; i <= n; i++){
Arrays.fill(city[i],Integer.MAX_VALUE/2);
}
for(int i = 0 ; i <e; i++){
input = buffer.readLine().split(" ");
int start = Integer.parseInt(input[0]);
int end = Integer.parseInt(input[1]);
int weight = Integer.parseInt(input[2]);
list[start].add(new int[]{end,weight});
}
for(int k = 1; k <=n; k++){
for(int i = 1; i <=n; i++){
for(int j = 1; j <=n; j++){
city[i][j] = Math.min(city[i][j],city[i][k] + city[k][j]);
}
}
}
for(int i = 1; i<=n; i++) {
dijkstra(i);
}
long max = Integer.MIN_VALUE;
for(int i = 1; i <=n; i++){
max = Math.max(max, city[i][x] + city[x][i]);
}
System.out.println(max);
} // main end
public static void dijkstra(int start){
visited = new boolean[n+1];
city[start][start] = 0;
PriorityQueue<int[]> pqueue = new PriorityQueue<>(Comparator.comparingInt(o->o[1]));
pqueue.offer(new int[] {start, 0});
while(!pqueue.isEmpty()) {
int [] cur = pqueue.poll();
int s = cur[0];
if(visited[s]) continue;
visited[s] = true;
for(int[] now : list[s]){
if(!visited[now[0]]){
if(city[start][now[0]] > city[start][cur[0]]+now[1]){
city[start][now[0]] = city[start][cur[0]]+now[1];
pqueue.offer(new int[]{now[0], city[start][now[0]]});
}
}
}
}// pqeue while end
}
}