Advertisement
Josif_tepe

Untitled

Feb 26th, 2023
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <map>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <stack>
  8. #include <queue>
  9. using namespace std;
  10. typedef long long ll;
  11. const int maxn = 2e5 + 10;
  12. vector<pair<int, int> > graph[maxn];
  13. struct node {
  14.     int idx;
  15.     ll cost;
  16.     node() {}
  17.     node(int _idx, ll _cost) {
  18.         idx = _idx;
  19.         cost = _cost;
  20.     }
  21.    
  22.     bool operator < (const node & tmp) const {
  23.         return cost > tmp.cost;
  24.     }
  25. };
  26. int main() {
  27.     ios_base::sync_with_stdio(false);
  28.     int n, m, k;
  29.     cin >> n >> m >> k;
  30.    
  31.     for(int i = 0; i < m; i++) {
  32.         int a, b, c;
  33.         cin >> a >> b >> c;
  34.         a--; b--;
  35.        
  36.         graph[a].push_back(make_pair(b, c));
  37.     }
  38.    
  39.     priority_queue<node> pq;
  40.     pq.push(node(0, 0));
  41.     vector<int> visited(n + 1, 0);
  42.    
  43.     int E = n - 1;
  44.     while(!pq.empty() and visited[E] < k) {
  45.         node c = pq.top();
  46.         pq.pop();
  47.        
  48.         visited[c.idx]++;
  49.         if(c.idx == E) {
  50.             cout << c.cost << " ";
  51.         }
  52.         if(visited[c.idx] <= k) {
  53.             for(int i = 0; i < (int) graph[c.idx].size(); i++) {
  54.                 int neighbour = graph[c.idx][i].first;
  55.                 ll weight = graph[c.idx][i].second;
  56.                 pq.push(node(neighbour, c.cost + weight));
  57.             }
  58.         }
  59.     }
  60.    
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement