|
| 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 int INF = (int)1e8+5; |
| 9 | + private static List<Node>[] graph; |
| 10 | + private static List<Integer> answer; |
| 11 | + private static int[] dist1, dist2, dist3, input; |
| 12 | + private static int n, m, t, s, g, h, temp; |
| 13 | +
|
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + int T = Integer.parseInt(br.readLine()); |
| 16 | +
|
| 17 | + while (T-->0) { |
| 18 | + init(); |
| 19 | + dijkstra(g, dist1); |
| 20 | + dijkstra(h, dist2); |
| 21 | + dijkstra(s, dist3); |
| 22 | +
|
| 23 | + for (int i = 0; i < t; i++) { |
| 24 | + int result = Math.min(dist1[s] + dist2[input[i]], dist1[input[i]] + dist2[s]); |
| 25 | + result += temp; |
| 26 | + if (result == dist3[input[i]]) { |
| 27 | + answer.add(input[i]); |
| 28 | + } |
| 29 | + } |
| 30 | +
|
| 31 | + Collections.sort(answer); |
| 32 | +
|
| 33 | + for (int i = 0; i < answer.size()-1; i++) { |
| 34 | + bw.write(answer.get(i) + " "); |
| 35 | + } |
| 36 | + bw.write(answer.get(answer.size()-1) + "\n"); |
| 37 | + } |
| 38 | + bw.flush(); |
| 39 | + bw.close(); |
| 40 | + br.close(); |
| 41 | + } |
| 42 | +
|
| 43 | + private static void init() throws IOException { |
| 44 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 45 | + n = Integer.parseInt(st.nextToken()); |
| 46 | + m = Integer.parseInt(st.nextToken()); |
| 47 | + t = Integer.parseInt(st.nextToken()); |
| 48 | +
|
| 49 | + graph = new List[n+1]; |
| 50 | + dist1 = new int[n+1]; |
| 51 | + dist2 = new int[n+1]; |
| 52 | + dist3 = new int[n+1]; |
| 53 | + input = new int[t]; |
| 54 | + answer = new ArrayList<>(); |
| 55 | +
|
| 56 | + st = new StringTokenizer(br.readLine()); |
| 57 | + s = Integer.parseInt(st.nextToken()); |
| 58 | + g = Integer.parseInt(st.nextToken()); |
| 59 | + h = Integer.parseInt(st.nextToken()); |
| 60 | +
|
| 61 | + for (int i = 0; i <= n; i++) { |
| 62 | + graph[i] = new ArrayList<>(); |
| 63 | + } |
| 64 | +
|
| 65 | + for (int i = 0; i < m; i++) { |
| 66 | + st = new StringTokenizer(br.readLine()); |
| 67 | + int a = Integer.parseInt(st.nextToken()); |
| 68 | + int b = Integer.parseInt(st.nextToken()); |
| 69 | + int cost = Integer.parseInt(st.nextToken()); |
| 70 | +
|
| 71 | + if ((a == g && b == h) || (a == h && b == g)) temp = cost; |
| 72 | +
|
| 73 | + graph[a].add(new Node(b, cost)); |
| 74 | + graph[b].add(new Node(a, cost)); |
| 75 | + } |
| 76 | +
|
| 77 | + for (int i = 0; i < t; i++) { |
| 78 | + input[i] = Integer.parseInt(br.readLine()); |
| 79 | + } |
| 80 | + } |
| 81 | +
|
| 82 | + private static void dijkstra(int start, int[] dist) { |
| 83 | + PriorityQueue<Node> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1.cost, o2.cost)); |
| 84 | + Arrays.fill(dist, INF); |
| 85 | + dist[start] = 0; |
| 86 | + pq.add(new Node(start, dist[start])); |
| 87 | +
|
| 88 | + while (!pq.isEmpty()) { |
| 89 | + Node current = pq.poll(); |
| 90 | +
|
| 91 | + if (current.cost > dist[current.dest]) continue; |
| 92 | +
|
| 93 | + for (Node next : graph[current.dest]) { |
| 94 | + int nCost = dist[current.dest] + next.cost; |
| 95 | +
|
| 96 | + if (nCost < dist[next.dest]) { |
| 97 | + dist[next.dest] = nCost; |
| 98 | + pq.add(new Node(next.dest, nCost)); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +
|
| 104 | + static class Node { |
| 105 | + int dest; |
| 106 | + int cost; |
| 107 | +
|
| 108 | + public Node(int dest, int cost) { |
| 109 | + this.dest = dest; |
| 110 | + this.cost = cost; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
0 commit comments