|
| 1 | +package Graph.P1854; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + static int n, m, k; |
| 9 | + static ArrayList<Node>[] graph; |
| 10 | + static PriorityQueue<Integer>[] D; |
| 11 | + |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + System.setIn(new FileInputStream("src/Graph/P1854/input.txt")); |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + |
| 17 | + n = stoi(st.nextToken()); |
| 18 | + m = stoi(st.nextToken()); |
| 19 | + k = stoi(st.nextToken()); |
| 20 | + |
| 21 | + graph = new ArrayList[n+1]; |
| 22 | + D = new PriorityQueue[n+1]; |
| 23 | + for (int i = 1; i <= n; i++) { |
| 24 | + graph[i] = new ArrayList<>(); |
| 25 | + D[i] = new PriorityQueue<>(((o1, o2) -> o2 - o1)); |
| 26 | + } |
| 27 | + |
| 28 | + while (m-- > 0) { |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + int a = stoi(st.nextToken()), b = stoi(st.nextToken()), c = stoi(st.nextToken()); |
| 31 | + graph[a].add(new Node(b, c)); |
| 32 | + } |
| 33 | + |
| 34 | + PriorityQueue<Node> pq = new PriorityQueue<>(((o1, o2) -> o1.dist - o2.dist)); |
| 35 | + pq.offer(new Node(1, 0)); |
| 36 | + D[1].offer(0); |
| 37 | + |
| 38 | + while (!pq.isEmpty()) { |
| 39 | + Node cur = pq.poll(); |
| 40 | + for (Node next : graph[cur.idx]) { |
| 41 | + if (D[next.idx].size() < k) { |
| 42 | + D[next.idx].offer(cur.dist + next.dist); |
| 43 | + pq.offer(new Node(next.idx, cur.dist + next.dist)); |
| 44 | + } else { |
| 45 | + if (D[next.idx].peek() > cur.dist + next.dist) { |
| 46 | + D[next.idx].poll(); |
| 47 | + D[next.idx].offer(cur.dist + next.dist); |
| 48 | + pq.offer(new Node(next.idx, cur.dist + next.dist)); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + for (int i = 1; i <= n; i++) { |
| 55 | + if (D[i].size() < k) System.out.println(-1); |
| 56 | + else System.out.println(D[i].peek()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + static class Node { |
| 61 | + int idx, dist; |
| 62 | + |
| 63 | + public Node(int idx, int dist) { |
| 64 | + this.idx = idx; |
| 65 | + this.dist = dist; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + static int stoi(String s) { return Integer.parseInt(s); } |
| 70 | +} |
0 commit comments