|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static final long INF = (long)2e10 + 10; |
| 9 | + private static List<Node>[] graph; |
| 10 | + private static long[] dist; |
| 11 | + private static int N, M, X, Y, Z; |
| 12 | +
|
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + init(); |
| 15 | +
|
| 16 | + dijkstra(X, 0); |
| 17 | + long result = dist[Y]; |
| 18 | + dijkstra(Y, 0); |
| 19 | + result += dist[Z]; |
| 20 | + if (result > INF) { |
| 21 | + bw.write("-1 "); |
| 22 | + } else { |
| 23 | + bw.write(result + " "); |
| 24 | + } |
| 25 | +
|
| 26 | + dijkstra(X, Y); |
| 27 | + if (dist[Z] == INF) { |
| 28 | + bw.write("-1 "); |
| 29 | + } else { |
| 30 | + bw.write(dist[Z] + " "); |
| 31 | + } |
| 32 | +
|
| 33 | + bw.flush(); |
| 34 | + bw.close(); |
| 35 | + br.close(); |
| 36 | + } |
| 37 | +
|
| 38 | + private static void init() throws IOException { |
| 39 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 40 | + N = Integer.parseInt(st.nextToken()); |
| 41 | + M = Integer.parseInt(st.nextToken()); |
| 42 | +
|
| 43 | + graph = new List[N+1]; |
| 44 | + dist = new long[N+1]; |
| 45 | + for (int i = 1; i <= N; i++) { |
| 46 | + graph[i] = new ArrayList<>(); |
| 47 | + } |
| 48 | +
|
| 49 | + for (int i = 0; i < M; i++) { |
| 50 | + st = new StringTokenizer(br.readLine()); |
| 51 | + int a = Integer.parseInt(st.nextToken()); |
| 52 | + int b = Integer.parseInt(st.nextToken()); |
| 53 | + int cost = Integer.parseInt(st.nextToken()); |
| 54 | +
|
| 55 | + graph[a].add(new Node(b, cost)); |
| 56 | + } |
| 57 | +
|
| 58 | + st = new StringTokenizer(br.readLine()); |
| 59 | + X = Integer.parseInt(st.nextToken()); |
| 60 | + Y = Integer.parseInt(st.nextToken()); |
| 61 | + Z = Integer.parseInt(st.nextToken()); |
| 62 | + } |
| 63 | +
|
| 64 | + private static void dijkstra(int start, int stopover) { |
| 65 | + PriorityQueue<Node> pq = new PriorityQueue<>((o1, o2) -> Long.compare(o1.cost, o2.cost)); |
| 66 | + Arrays.fill(dist, INF); |
| 67 | + dist[start] = 0; |
| 68 | + pq.add(new Node(start, 0)); |
| 69 | +
|
| 70 | + while (!pq.isEmpty()) { |
| 71 | + Node current = pq.poll(); |
| 72 | +
|
| 73 | + if (current.cost > dist[current.dest]) continue; |
| 74 | +
|
| 75 | + for (Node next : graph[current.dest]) { |
| 76 | + if (next.dest == stopover) continue; |
| 77 | + if (current.cost + next.cost < dist[next.dest]) { |
| 78 | + dist[next.dest] = current.cost + next.cost; |
| 79 | + pq.add(new Node(next.dest, dist[next.dest])); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +
|
| 85 | + static class Node { |
| 86 | + int dest; |
| 87 | + long cost; |
| 88 | +
|
| 89 | + Node(int dest, long cost) { |
| 90 | + this.dest = dest; |
| 91 | + this.cost = cost; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
0 commit comments