|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.PriorityQueue; |
| 9 | + |
| 10 | +public class Main { |
| 11 | + private static BufferedReader br; |
| 12 | + private static int m; |
| 13 | + private static int n; |
| 14 | + |
| 15 | + static class Node implements Comparable<Node> { |
| 16 | + int v; |
| 17 | + int w; |
| 18 | + boolean prevDash; |
| 19 | + |
| 20 | + public Node(int v, int w) { |
| 21 | + this.v = v; |
| 22 | + this.w = w; |
| 23 | + this.prevDash = false; |
| 24 | + } |
| 25 | + |
| 26 | + public Node(int v, int w, boolean prevDash) { |
| 27 | + this.v = v; |
| 28 | + this.w = w; |
| 29 | + this.prevDash = prevDash; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public int compareTo(Main.Node o) { |
| 34 | + return this.w - o.w; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static int[] dijkstra(List<List<Node>> graph, int start) { |
| 39 | + int n = graph.size(); |
| 40 | + int[] dist = new int[n]; |
| 41 | + Arrays.fill(dist, Integer.MAX_VALUE); |
| 42 | + dist[start] = 0; |
| 43 | + PriorityQueue<Node> pq = new PriorityQueue<>(); |
| 44 | + pq.offer(new Node(start, 0)); |
| 45 | + |
| 46 | + while (!pq.isEmpty()) { |
| 47 | + Node cn = pq.poll(); |
| 48 | + int cv = cn.v; |
| 49 | + int cw = cn.w; |
| 50 | + if (dist[cv] < cw) continue; |
| 51 | + |
| 52 | + for (Node neighbor : graph.get(cv)) { |
| 53 | + int nv = neighbor.v; |
| 54 | + int nw = neighbor.w; |
| 55 | + if (dist[nv] > dist[cv] + nw) { |
| 56 | + dist[nv] = dist[cv] + nw; |
| 57 | + pq.offer(new Node(nv, dist[nv])); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return dist; |
| 62 | + } |
| 63 | + |
| 64 | + //달빛늑대 다익스트라 |
| 65 | + public static int[][] dijkstraWolf(List<List<Node>> graph, int start) { |
| 66 | + int[][] dist = new int[2][graph.size()]; |
| 67 | + for (int i = 0; i < 2; i++) { |
| 68 | + Arrays.fill(dist[i], Integer.MAX_VALUE); |
| 69 | + } |
| 70 | + PriorityQueue<Node> pq = new PriorityQueue<>(); |
| 71 | + dist[0][start] = 0; |
| 72 | + pq.offer(new Node(start, 0, false)); |
| 73 | + |
| 74 | + while (!pq.isEmpty()) { |
| 75 | + Node cn = pq.poll(); |
| 76 | + int cv = cn.v; |
| 77 | + int cw = cn.w; |
| 78 | + boolean prevDash = cn.prevDash; |
| 79 | + int state = prevDash ? 1 : 0; |
| 80 | + if (dist[state][cv] < cw) continue; |
| 81 | + |
| 82 | + for (Node neighbor : graph.get(cv)) { |
| 83 | + int nv = neighbor.v; |
| 84 | + int nw = neighbor.w; |
| 85 | + int nextW = prevDash ? cw + nw : cw + nw / 2; |
| 86 | + if (dist[1 - state][nv] > nextW) { |
| 87 | + dist[1 - state][nv] = nextW; |
| 88 | + pq.offer(new Node(nv, nextW, !prevDash)); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return dist; |
| 94 | + } |
| 95 | + |
| 96 | + public static void main(String[] args) throws IOException { |
| 97 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 98 | + |
| 99 | + String[] temp = br.readLine().split(" "); |
| 100 | + n = Integer.parseInt(temp[0]); |
| 101 | + m = Integer.parseInt(temp[1]); |
| 102 | + |
| 103 | + List<List<Node>> graph = new ArrayList<>(); |
| 104 | + for (int i = 0; i <= n; i++) { |
| 105 | + graph.add(new ArrayList<Node>()); |
| 106 | + } |
| 107 | + for (int i = 0; i < m; i++) { |
| 108 | + temp = br.readLine().split(" "); |
| 109 | + int a = Integer.parseInt(temp[0]); |
| 110 | + int b = Integer.parseInt(temp[1]); |
| 111 | + int w = Integer.parseInt(temp[2]); |
| 112 | + graph.get(a).add(new Node(b, w*2));//계산 용이를 위해 애초에 곱하기 2 |
| 113 | + graph.get(b).add(new Node(a, w*2)); |
| 114 | + } |
| 115 | + |
| 116 | + // 달빛여우 다익 진행 |
| 117 | + int[] foxDist = dijkstra(graph, 1); |
| 118 | + |
| 119 | + // 달빛늑대 다익진행(2차원 dist배열 사용) |
| 120 | + int[][] wolfDist = dijkstraWolf(graph, 1); |
| 121 | + |
| 122 | + int count = 0; |
| 123 | + for (int i = 2; i <= n; i++) { |
| 124 | + int wolfMin = Math.min(wolfDist[0][i], wolfDist[1][i]); |
| 125 | + if (foxDist[i] < wolfMin) { |
| 126 | + count++; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + System.out.println(count); |
| 131 | + br.close(); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +``` |
0 commit comments