|
| 1 | +/* |
| 2 | +Problem: Flight Routes (CSES Problem Set 1196) |
| 3 | +Category: Graph Algorithms |
| 4 | +Difficulty: Medium |
| 5 | +Time Complexity: O(m * log(n * k)) |
| 6 | +Space Complexity: O(n * k) |
| 7 | +
|
| 8 | +Approach: |
| 9 | +This is a variant of Dijkstra’s algorithm where we need the k shortest paths |
| 10 | +from the source (1) to destination (n), allowing repeated nodes in routes. |
| 11 | +
|
| 12 | +We maintain for each node a max-heap of up to k shortest distances found so far. |
| 13 | +We process nodes in increasing order of distance (via a min-heap). |
| 14 | +If a new smaller distance is found, we push it into that node's heap. |
| 15 | +If the heap exceeds k, we remove the largest (keeping only k smallest). |
| 16 | +
|
| 17 | +This avoids sorting every time (unlike a vector-based approach), |
| 18 | +resulting in much faster performance for large graphs. |
| 19 | +
|
| 20 | +Key Insights: |
| 21 | +- Standard Dijkstra only tracks one best distance per node; here we track up to k. |
| 22 | +- Using a max-heap per node allows O(log k) insertion/removal. |
| 23 | +- We only store k distances per node → O(n * k) memory. |
| 24 | +- Since k ≤ 10, the algorithm is very efficient. |
| 25 | +
|
| 26 | +Edge Cases: |
| 27 | +- Multiple edges between the same nodes. |
| 28 | +- Repeated visits to the same node with different costs. |
| 29 | +- Large weights (use long long). |
| 30 | +*/ |
| 31 | + |
| 32 | +#include <bits/stdc++.h> |
| 33 | +using namespace std; |
| 34 | + |
| 35 | +#define int long long |
| 36 | +#define pii pair<long long, int> |
| 37 | + |
| 38 | +int32_t main() { |
| 39 | + ios::sync_with_stdio(false); |
| 40 | + cin.tie(nullptr); |
| 41 | + |
| 42 | + int n, m, k; |
| 43 | + cin >> n >> m >> k; |
| 44 | + |
| 45 | + vector<vector<pair<int,int>>> adj(n + 1); |
| 46 | + for (int i = 0; i < m; i++) { |
| 47 | + int a, b, c; |
| 48 | + cin >> a >> b >> c; |
| 49 | + adj[a].push_back({b, c}); |
| 50 | + } |
| 51 | + |
| 52 | + // Max-heaps to store up to k shortest distances per node |
| 53 | + vector<priority_queue<long long>> dist(n + 1); |
| 54 | + |
| 55 | + // Min-heap for Dijkstra: {distance, node} |
| 56 | + priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> pq; |
| 57 | + |
| 58 | + pq.push({0, 1}); |
| 59 | + dist[1].push(0); |
| 60 | + |
| 61 | + while (!pq.empty()) { |
| 62 | + auto [currDist, node] = pq.top(); |
| 63 | + pq.pop(); |
| 64 | + |
| 65 | + // Skip if this path is not among the k smallest for this node |
| 66 | + if (dist[node].top() < currDist && (int)dist[node].size() == k) |
| 67 | + continue; |
| 68 | + |
| 69 | + for (auto &[next, wt] : adj[node]) { |
| 70 | + long long newDist = currDist + wt; |
| 71 | + |
| 72 | + // If less than k distances stored or this one is smaller than the largest |
| 73 | + if ((int)dist[next].size() < k || newDist < dist[next].top()) { |
| 74 | + dist[next].push(newDist); |
| 75 | + pq.push({newDist, next}); |
| 76 | + if ((int)dist[next].size() > k) |
| 77 | + dist[next].pop(); // Keep only k smallest distances |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Collect all distances from max-heap of destination node and sort ascending |
| 83 | + vector<long long> ans; |
| 84 | + while (!dist[n].empty()) { |
| 85 | + ans.push_back(dist[n].top()); |
| 86 | + dist[n].pop(); |
| 87 | + } |
| 88 | + sort(ans.begin(), ans.end()); |
| 89 | + |
| 90 | + for (auto &d : ans) |
| 91 | + cout << d << " "; |
| 92 | + cout << "\n"; |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
0 commit comments