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