DuongNhi99

ROADS

Dec 10th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. const int N = 105;
  6. const int oo = 1e9 + 7;
  7.  
  8. struct node {
  9.     int root, dist, cost;
  10. };
  11.  
  12. bool operator > (node a, node b) {
  13.     return a.cost > b.cost;
  14. }
  15.  
  16. int k, n, m;
  17. vector<node> a[N];
  18.  
  19. int d[N];
  20.  
  21. void dijkstra() {
  22.     priority_queue<node, vector<node>, greater<node>> pq;
  23.     fill(d + 1, d + n + 1, oo);
  24.     pq.push({1, 0, 0});
  25.  
  26.     while(!pq.empty()) {
  27.         int u = pq.top().root;
  28.         int du = pq.top().dist;
  29.         int costu = pq.top().cost;
  30.         pq.pop();
  31.  
  32.         if(du >= d[u]) continue;
  33.  
  34.         d[u] = du;
  35.         for(int i = 0; i < a[u].size(); i++) {
  36.             int v = a[u][i].root;
  37.             int w = a[u][i].dist;
  38.             int c = a[u][i].cost;
  39.  
  40.             if(costu + c > k) continue;
  41.             pq.push({v, du + w, costu + c});
  42.         }
  43.     }
  44. }
  45.  
  46. int main() {
  47.     //freopen("in.txt", "r", stdin);
  48.     //freopen("ROADS.inp", "r", stdin);
  49.     //freopen("ROADS.out", "w", stdout);
  50.     ios_base::sync_with_stdio(false);
  51.     cin.tie(NULL); cout.tie(NULL);
  52.  
  53.     int t; cin >> t;
  54.     while(t--) {
  55.         cin >> k >> n >> m;
  56.         for (int i = 1; i <= n; i++)
  57.             a[i].clear();
  58.  
  59.         for(int i = 1; i <= m; i++) {
  60.             int u, v, w, c;
  61.             cin >> u >> v >> w >> c;
  62.             a[u].push_back({v, w, c});
  63.         }
  64.  
  65.         dijkstra();
  66.  
  67.         if(d[n] == oo) d[n] = -1;
  68.         cout << d[n] << '\n';
  69.     }
  70.  
  71.     return 0;
  72. }
  73.  
Add Comment
Please, Sign In to add comment