tuki2501

roads.cpp

Nov 10th, 2021 (edited)
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ar array
  5.  
  6. const int N = 105;
  7. const int K = 10005;
  8. const int INF = 0x3f3f3f3f;
  9.  
  10. struct road {
  11.   int v, w, c;
  12.   road() { v = w = c = 0; }
  13.   road(int v, int w, int c): v(v), w(w), c(c) {}
  14.   bool operator()(road a, road b) {
  15.     if (a.c == b.c) return a.w > b.w;
  16.     return a.c > b.c;
  17.   }
  18. };
  19.  
  20. int n, m, k;
  21. vector<road> adj[N];
  22. int dist[N], cost[N];
  23.  
  24. void solve() {
  25.   cin >> k >> n >> m;
  26.   for (int i = 1; i <= n; i++) {
  27.     adj[i].clear();
  28.   }
  29.   for (int i = 0; i < m; i++) {
  30.     int u, v, w, c;
  31.     cin >> u >> v >> w >> c;
  32.     adj[u].push_back(road(v, w, c));
  33.   }
  34.   memset(cost, 0x00, sizeof(cost));
  35.   memset(dist, 0x3f, sizeof(dist));
  36.   priority_queue<road, vector<road>, road> pq;
  37.   pq.push({1, 0, 0});
  38.   while (pq.size()) {
  39.     auto i = pq.top(); pq.pop();
  40.     int v = i.v, w = i.w, c = i.c;
  41.     if (w >= dist[v] || c > k) continue;
  42.     dist[v] = w;
  43.     cost[v] = c;
  44.     for (auto &j : adj[i.v]) {
  45.       pq.push(road(j.v, i.w + j.w, i.c + j.c));
  46.     }
  47.   }
  48.   if (dist[n] == INF) dist[n] = -1;
  49.   cout << dist[n] << '\n';
  50. }
  51.  
  52. int main() {
  53.   cin.tie(0)->sync_with_stdio(0);
  54.   int T; cin >> T;
  55.   while (T--) solve();
  56. }
  57.  
Add Comment
Please, Sign In to add comment