|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BJ_13911_집_구하기 { |
| 6 | + |
| 7 | + private static final long INF = 9876543210L; |
| 8 | + |
| 9 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + private static StringTokenizer st; |
| 12 | + |
| 13 | + private static int V, E; |
| 14 | + private static int M, x; |
| 15 | + private static int S, y; |
| 16 | + |
| 17 | + private static long[] mcdonalds; |
| 18 | + private static long[] starbucks; |
| 19 | + private static boolean[] isMcdonalds; |
| 20 | + private static boolean[] isStarbucks; |
| 21 | + |
| 22 | + private static Queue<Node> pq; |
| 23 | + private static List<Node>[] graph; |
| 24 | + |
| 25 | + private static class Node implements Comparable<Node> { |
| 26 | + int from; |
| 27 | + int to; |
| 28 | + long dist; |
| 29 | + |
| 30 | + Node(int from, int to, long dist) { |
| 31 | + this.from = from; |
| 32 | + this.to = to; |
| 33 | + this.dist = dist; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public int compareTo(Node o) { |
| 38 | + return Long.compare(this.dist, o.dist); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public static void main(String[] args) throws IOException { |
| 43 | + init(); |
| 44 | + sol(); |
| 45 | + } |
| 46 | + |
| 47 | + private static void init() throws IOException { |
| 48 | + st = new StringTokenizer(br.readLine()); |
| 49 | + V = Integer.parseInt(st.nextToken()); |
| 50 | + E = Integer.parseInt(st.nextToken()); |
| 51 | + |
| 52 | + graph = new List[V + 1]; |
| 53 | + for (int i = 1; i <= V; i++) { |
| 54 | + graph[i] = new ArrayList<>(); |
| 55 | + } |
| 56 | + |
| 57 | + for (int i = 1; i <= E; i++) { |
| 58 | + st = new StringTokenizer(br.readLine()); |
| 59 | + |
| 60 | + int from = Integer.parseInt(st.nextToken()); |
| 61 | + int to = Integer.parseInt(st.nextToken()); |
| 62 | + int weight = Integer.parseInt(st.nextToken()); |
| 63 | + |
| 64 | + graph[from].add(new Node(from, to, weight)); |
| 65 | + graph[to].add(new Node(to, from, weight)); |
| 66 | + } |
| 67 | + |
| 68 | + mcdonalds = new long[V + 1]; |
| 69 | + starbucks = new long[V + 1]; |
| 70 | + isMcdonalds = new boolean[V + 1]; |
| 71 | + isStarbucks = new boolean[V + 1]; |
| 72 | + |
| 73 | + pq = new PriorityQueue<>(); |
| 74 | + } |
| 75 | + |
| 76 | + private static void sol() throws IOException { |
| 77 | + // Multisource Dijkstra |
| 78 | + st = new StringTokenizer(br.readLine()); |
| 79 | + M = Integer.parseInt(st.nextToken()); |
| 80 | + x = Integer.parseInt(st.nextToken()); |
| 81 | + dijkstra(mcdonalds, isMcdonalds, M); |
| 82 | + |
| 83 | + // Multisource Dijkstra |
| 84 | + st = new StringTokenizer(br.readLine()); |
| 85 | + S = Integer.parseInt(st.nextToken()); |
| 86 | + y = Integer.parseInt(st.nextToken()); |
| 87 | + dijkstra(starbucks, isStarbucks, S); |
| 88 | + |
| 89 | + long ans = INF; |
| 90 | + for (int i = 1; i <= V; i++) { |
| 91 | + if (!isMcdonalds[i] && !isStarbucks[i] && mcdonalds[i] <= x && starbucks[i] <= y) { |
| 92 | + ans = Math.min(ans, mcdonalds[i] + starbucks[i]); |
| 93 | + } |
| 94 | + } |
| 95 | + bw.write((ans == INF ? -1 : ans) + ""); |
| 96 | + bw.flush(); |
| 97 | + bw.close(); |
| 98 | + br.close(); |
| 99 | + } |
| 100 | + |
| 101 | + private static void dijkstra(long[] dist, boolean[] isStore, int cnt) throws IOException { |
| 102 | + pq.clear(); |
| 103 | + Arrays.fill(dist, INF); |
| 104 | + |
| 105 | + st = new StringTokenizer(br.readLine()); |
| 106 | + for (int i = 0; i < cnt; i++) { |
| 107 | + int start = Integer.parseInt(st.nextToken()); |
| 108 | + isStore[start] = true; |
| 109 | + dist[start] = 0; |
| 110 | + pq.offer(new Node(start, start, 0)); |
| 111 | + } |
| 112 | + |
| 113 | + while (!pq.isEmpty()) { |
| 114 | + Node cur = pq.poll(); |
| 115 | + |
| 116 | + if (cur.dist > dist[cur.to]) continue; |
| 117 | + |
| 118 | + for (Node n : graph[cur.to]) { |
| 119 | + long nDist = dist[cur.to] + n.dist; |
| 120 | + |
| 121 | + if (nDist < dist[n.to]) { |
| 122 | + dist[n.to] = nDist; |
| 123 | + pq.offer(new Node(n.from, n.to, nDist)); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +} |
| 130 | +``` |
0 commit comments