fork(2) download
  1. #include <bits/stdc++.h>
  2. #define endl "\n"
  3. using namespace std;
  4. #define int long long int
  5.  
  6. int n, m, k;
  7. vector<vector<pair<int,int>>> g;
  8. vector<vector<int>> dist;
  9. const int INF = 9e15;
  10.  
  11. void dij()
  12. {
  13.  
  14. priority_queue <
  15. pair<int,int>,
  16. vector<pair<int,int>>,
  17. greater<pair<int,int>>
  18. > pq;
  19.  
  20. pq.push({0,1});
  21.  
  22. while(!pq.empty())
  23. {
  24. int u = pq.top().second;
  25. int d = pq.top().first;
  26. pq.pop();
  27.  
  28. if(dist[u][k-1] < d) continue;
  29.  
  30. for(auto e: g[u])
  31. {
  32. int v = e.first;
  33. int c = e.second;
  34.  
  35. if(dist[v][k-1] > c+d)
  36. {
  37. dist[v][k-1] = c+d;
  38. pq.push({dist[v][k-1], v});
  39. sort(dist[v].begin(), dist[v].end());
  40. }
  41. }
  42. }
  43. }
  44.  
  45. int32_t main()
  46. {
  47. ios_base::sync_with_stdio(false);
  48. cin.tie(NULL);
  49. cin >> n >> m >> k;
  50. g.resize(n+1);
  51. dist.resize(n+1);
  52. for(int i = 1; i <= n; i++)
  53. {
  54. dist[i].resize(k);
  55. for(int j = 0; j <k; ++j)
  56. {
  57. dist[i][j] = INF;
  58. }
  59. }
  60. dist[1][0] = 0;
  61. for(int i = 0; i < m; ++i)
  62. {
  63. int u, v, c;
  64. cin >> u >> v >> c;
  65. g[u].push_back({v,c});
  66. }
  67.  
  68. dij();
  69.  
  70. for(int i = 0; i < k; ++i)
  71. {
  72. cout << dist[n][i] << " ";
  73. }
  74. }
Runtime error #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
Standard output is empty