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