|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int N, M, X; |
| 7 | + static ArrayList<ArrayList<Node>> graph; |
| 8 | + static ArrayList<ArrayList<Node>> reverseGraph; |
| 9 | + static int[] distToX; |
| 10 | + static int[] distFromX; |
| 11 | + |
| 12 | + static class Node { |
| 13 | + int vertex; |
| 14 | + int cost; |
| 15 | + |
| 16 | + Node(int vertex, int cost) { |
| 17 | + this.vertex = vertex; |
| 18 | + this.cost = cost; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public static void main(String[] args) throws IOException { |
| 23 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 24 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 25 | + |
| 26 | + N = Integer.parseInt(st.nextToken()); |
| 27 | + M = Integer.parseInt(st.nextToken()); |
| 28 | + X = Integer.parseInt(st.nextToken()); |
| 29 | + |
| 30 | + graph = new ArrayList<>(); |
| 31 | + reverseGraph = new ArrayList<>(); |
| 32 | + |
| 33 | + for (int i = 0; i <= N; i++) { |
| 34 | + graph.add(new ArrayList<>()); |
| 35 | + reverseGraph.add(new ArrayList<>()); |
| 36 | + } |
| 37 | + |
| 38 | + for (int i = 0; i < M; i++) { |
| 39 | + st = new StringTokenizer(br.readLine()); |
| 40 | + int start = Integer.parseInt(st.nextToken()); |
| 41 | + int end = Integer.parseInt(st.nextToken()); |
| 42 | + int time = Integer.parseInt(st.nextToken()); |
| 43 | + |
| 44 | + graph.get(start).add(new Node(end, time)); |
| 45 | + reverseGraph.get(end).add(new Node(start, time)); |
| 46 | + } |
| 47 | + |
| 48 | + distToX = new int[N + 1]; |
| 49 | + distFromX = new int[N + 1]; |
| 50 | + |
| 51 | + dijkstra(X, graph, distFromX); |
| 52 | + dijkstra(X, reverseGraph, distToX); |
| 53 | + |
| 54 | + int maxTime = 0; |
| 55 | + for (int i = 1; i <= N; i++) { |
| 56 | + if (i != X) { |
| 57 | + maxTime = Math.max(maxTime, distToX[i] + distFromX[i]); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + System.out.println(maxTime); |
| 62 | + } |
| 63 | + |
| 64 | + static void dijkstra(int start, ArrayList<ArrayList<Node>> targetGraph, int[] dist) { |
| 65 | + Arrays.fill(dist, Integer.MAX_VALUE); |
| 66 | + PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> Integer.compare(a.cost, b.cost)); |
| 67 | + |
| 68 | + dist[start] = 0; |
| 69 | + pq.offer(new Node(start, 0)); |
| 70 | + |
| 71 | + while (!pq.isEmpty()) { |
| 72 | + Node current = pq.poll(); |
| 73 | + int currentVertex = current.vertex; |
| 74 | + int currentCost = current.cost; |
| 75 | + |
| 76 | + if (currentCost > dist[currentVertex]) continue; |
| 77 | + |
| 78 | + for (Node next : targetGraph.get(currentVertex)) { |
| 79 | + int nextVertex = next.vertex; |
| 80 | + int nextCost = currentCost + next.cost; |
| 81 | + |
| 82 | + if (nextCost < dist[nextVertex]) { |
| 83 | + dist[nextVertex] = nextCost; |
| 84 | + pq.offer(new Node(nextVertex, nextCost)); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
0 commit comments