|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BJ_1162_도로포장 { |
| 6 | + |
| 7 | + private static final long INF = 50_000_000_001L; |
| 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 N, M, K; |
| 14 | + private static long[][] dist; |
| 15 | + private static List<Node>[] graph; |
| 16 | + private static Queue<Node> pq; |
| 17 | + |
| 18 | + private static class Node implements Comparable<Node> { |
| 19 | + int to; |
| 20 | + long cost; |
| 21 | + int cnt; |
| 22 | + |
| 23 | + public Node(int to, long cost, int cnt) { |
| 24 | + this.to = to; |
| 25 | + this.cost = cost; |
| 26 | + this.cnt = cnt; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public int compareTo(Node o) { |
| 31 | + return Long.compare(this.cost, o.cost); |
| 32 | + } |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + public static void main(String[] args) throws IOException { |
| 37 | + init(); |
| 38 | + sol(); |
| 39 | + } |
| 40 | + |
| 41 | + private static void init() throws IOException { |
| 42 | + st = new StringTokenizer(br.readLine()); |
| 43 | + N = Integer.parseInt(st.nextToken()); |
| 44 | + M = Integer.parseInt(st.nextToken()); |
| 45 | + K = Integer.parseInt(st.nextToken()); |
| 46 | + |
| 47 | + graph = new List[N + 1]; |
| 48 | + for (int i = 1; i <= N; i++) { |
| 49 | + graph[i] = new ArrayList<>(); |
| 50 | + } |
| 51 | + |
| 52 | + for (int i = 0; i < M; i++) { |
| 53 | + st = new StringTokenizer(br.readLine()); |
| 54 | + int a = Integer.parseInt(st.nextToken()); |
| 55 | + int b = Integer.parseInt(st.nextToken()); |
| 56 | + int c = Integer.parseInt(st.nextToken()); |
| 57 | + |
| 58 | + graph[a].add(new Node(b, c, 0)); |
| 59 | + graph[b].add(new Node(a, c, 0)); |
| 60 | + } |
| 61 | + |
| 62 | + dist = new long[N + 1][K + 1]; |
| 63 | + for (int i = 1; i <= N; i++) { |
| 64 | + Arrays.fill(dist[i], INF); |
| 65 | + } |
| 66 | + |
| 67 | + pq = new PriorityQueue<>(); |
| 68 | + } |
| 69 | + |
| 70 | + private static void sol() throws IOException { |
| 71 | + pq.offer(new Node(1, 0, 0)); |
| 72 | + dist[1][0] = 0; |
| 73 | + |
| 74 | + while (!pq.isEmpty()) { |
| 75 | + Node cur = pq.poll(); |
| 76 | + |
| 77 | + if (cur.cost > dist[cur.to][cur.cnt]) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + for (Node next : graph[cur.to]) { |
| 82 | + long nextCost = dist[cur.to][cur.cnt] + next.cost; |
| 83 | + |
| 84 | + // not exclude |
| 85 | + if (nextCost < dist[next.to][cur.cnt]) { |
| 86 | + dist[next.to][cur.cnt] = nextCost; |
| 87 | + pq.offer(new Node(next.to, nextCost, cur.cnt)); |
| 88 | + } |
| 89 | + |
| 90 | + if (cur.cnt < K && cur.cost < dist[next.to][cur.cnt + 1]) { |
| 91 | + dist[next.to][cur.cnt + 1] = cur.cost; |
| 92 | + pq.offer(new Node(next.to, cur.cost, cur.cnt + 1)); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + long ans = INF; |
| 98 | + for (int i = 0; i <= K; i++) { |
| 99 | + ans = Math.min(ans, dist[N][i]); |
| 100 | + } |
| 101 | + bw.write(ans + ""); |
| 102 | + bw.flush(); |
| 103 | + bw.close(); |
| 104 | + br.close(); |
| 105 | + } |
| 106 | + |
| 107 | +} |
| 108 | +``` |
0 commit comments