-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBOJ1238.java
More file actions
83 lines (71 loc) · 3.02 KB
/
BOJ1238.java
File metadata and controls
83 lines (71 loc) · 3.02 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
80
81
82
83
import java.io.*;
import java.util.*;
public class BOJ1238 {
static class Edge implements Comparable<Edge> {
int to, weight;
Edge(int to, int weight) {
this.to = to;
this.weight = weight;
}
public int compareTo(Edge o) {
return this.weight - o.weight;
}
}
static int INF = Integer.MAX_VALUE;
public static int[] dijkstra(int start, ArrayList<Edge>[] graph, int n) {
int[] dist = new int[n + 1];
Arrays.fill(dist, INF);
dist[start] = 0;
PriorityQueue<Edge> pq = new PriorityQueue<>();
pq.add(new Edge(start, 0));
while (!pq.isEmpty()) {
Edge current = pq.poll();
int cur = current.to;
int curDist = current.weight;
// 현재 저장된 거리보다 큰 경우 스킵
if (curDist > dist[cur]) continue;
for (Edge edge : graph[cur]) {
int next = edge.to;
int nextDist = curDist + edge.weight;
if (nextDist < dist[next]) {
dist[next] = nextDist;
pq.add(new Edge(next, nextDist));
}
}
}
return dist;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken()); // 정점 개수
int M = Integer.parseInt(st.nextToken()); // 간선 개수
int X = Integer.parseInt(st.nextToken()); // 파티가 열리는 정점
// 원래 그래프와 간선을 뒤집은 역방향 그래프 생성
ArrayList<Edge>[] graph = new ArrayList[N + 1];
ArrayList<Edge>[] revGraph = new ArrayList[N + 1];
for (int i = 1; i <= N; i++) {
graph[i] = new ArrayList<>();
revGraph[i] = new ArrayList<>();
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
graph[from].add(new Edge(to, weight));
// 역방향 그래프: 도착지를 from으로, from을 to로 저장
revGraph[to].add(new Edge(from, weight));
}
// X에서 각 정점까지 최단 경로
int[] distFromX = dijkstra(X, graph, N);
// 각 정점에서 x로 가는 최단 경로
int[] distToX = dijkstra(X, revGraph, N);
// 모든 정점에 대해 왕복 시간의 최댓값 계산
int maxTime = 0;
for (int i = 1; i <= N; i++) {
maxTime = Math.max(maxTime, distFromX[i] + distToX[i]);
}
System.out.println(maxTime);
}
}