fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. const int N = 1e5 + 5;
  12.  
  13. int n, m, k;
  14. vector<ii> adj[N];
  15.  
  16. struct Node {
  17. int u; ll d;
  18. bool operator<(Node other) const {
  19. return d > other.d;
  20. }
  21. };
  22.  
  23. int cnt[N];
  24. ll dist[N][11]; // dist[u][i] = Đường đi ngắn thứ i từ s đến u
  25.  
  26. void dijkstra(int s) {
  27. priority_queue<Node> pq;
  28. pq.push({s, 0});
  29.  
  30. while (!pq.empty()) {
  31. Node front = pq.top(); pq.pop();
  32. int u = front.u; ll d = front.d;
  33.  
  34. cnt[u]++;
  35. if (cnt[u] > k) continue;
  36. dist[u][cnt[u]] = d;
  37.  
  38. for (ii v : adj[u]) {
  39. pq.push({v.first, d + v.second});
  40. }
  41. }
  42. } // O(k*m*log(k*m))
  43.  
  44. int main() {
  45. ios::sync_with_stdio(false);
  46. cin.tie(nullptr);
  47. cin >> n >> m >> k;
  48.  
  49. for (int i = 0; i < m; i++) {
  50. int u, v, w;
  51. cin >> u >> v >> w;
  52. adj[u].push_back({v, w});
  53. }
  54.  
  55. dijkstra(1);
  56.  
  57. for (int i = 1; i <= k; i++) cout << dist[n][i] << ' ';
  58. }
Success #stdin #stdout 0.01s 5920KB
stdin
4 6 3
1 2 1
1 3 3
2 3 2
2 4 6
3 2 8
3 4 1
stdout
4 4 7