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