|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Main{ |
| 6 | + static class Edge { |
| 7 | + int to; |
| 8 | + int cost; |
| 9 | + |
| 10 | + public Edge(int to, int cost) { |
| 11 | + this.to = to; |
| 12 | + this.cost = cost; |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 18 | + StringBuilder sb = new StringBuilder(); |
| 19 | + |
| 20 | + int repeat = Integer.parseInt(br.readLine()); |
| 21 | + for(int test=0; test<repeat; test++) { |
| 22 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 23 | + int n = Integer.parseInt(st.nextToken()); |
| 24 | + int m = Integer.parseInt(st.nextToken()); |
| 25 | + int t = Integer.parseInt(st.nextToken()); |
| 26 | + |
| 27 | + List<Edge>[] graph = new ArrayList[n+1]; |
| 28 | + for(int i=1; i<=n; i++) { |
| 29 | + graph[i] = new ArrayList<>(); |
| 30 | + } |
| 31 | + |
| 32 | + st = new StringTokenizer(br.readLine()); |
| 33 | + int start = Integer.parseInt(st.nextToken()); |
| 34 | + int x1 = Integer.parseInt(st.nextToken()); |
| 35 | + int x2 = Integer.parseInt(st.nextToken()); |
| 36 | + |
| 37 | + for(int i=0; i<m; i++) { |
| 38 | + st = new StringTokenizer(br.readLine()); |
| 39 | + int a = Integer.parseInt(st.nextToken()); |
| 40 | + int b = Integer.parseInt(st.nextToken()); |
| 41 | + int c = Integer.parseInt(st.nextToken()); |
| 42 | + if((a == x1 && b == x2) || (a == x2 && b == x1)){ |
| 43 | + graph[a].add(new Edge(b, c*2 - 1)); |
| 44 | + graph[b].add(new Edge(a, c*2 - 1)); |
| 45 | + }else{ |
| 46 | + graph[a].add(new Edge(b, c*2)); |
| 47 | + graph[b].add(new Edge(a, c*2)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + int[] ends = new int[t]; |
| 52 | + for(int i=0; i<t; i++) { |
| 53 | + ends[i] = Integer.parseInt(br.readLine()); |
| 54 | + } |
| 55 | + |
| 56 | + int[] dist = new int[n+1]; |
| 57 | + Arrays.fill(dist, (int)1e9); |
| 58 | + PriorityQueue<Edge> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1.cost, o2.cost)); |
| 59 | + pq.offer(new Edge(start, 0)); |
| 60 | + dist[start] = 0; |
| 61 | + |
| 62 | + while(!pq.isEmpty()) { |
| 63 | + Edge current = pq.poll(); |
| 64 | + |
| 65 | + if(dist[current.to] < current.cost) continue; |
| 66 | + |
| 67 | + for(Edge edge : graph[current.to]) { |
| 68 | + int newCost = current.cost + edge.cost; |
| 69 | + if(dist[edge.to] <= newCost) continue; |
| 70 | + dist[edge.to] = newCost; |
| 71 | + pq.offer(new Edge(edge.to, newCost)); |
| 72 | + } |
| 73 | + } |
| 74 | + Arrays.sort(ends); |
| 75 | + for(int end : ends) { |
| 76 | + int cost = dist[end]; |
| 77 | + if(cost%2 == 0) continue; |
| 78 | + sb.append(end).append(" "); |
| 79 | + } |
| 80 | + sb.append("\n"); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + System.out.println(sb.toString().trim()); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
0 commit comments