Advertisement
newb_ie

LightOJ - New Traffic System

Mar 12th, 2021
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 10100;
  5. vector<pair<pair<int,int>,int>> adj[maxN];
  6. int cost[maxN];
  7.  
  8. void dijkstra (int source,int n,int d) {
  9.     for (int i = 0; i <= n; ++i) cost[i] = (int) 1e7;
  10.     cost[source] = 0;
  11.     priority_queue<pair<pair<int,int>,int>> pq;
  12.     pq.push(make_pair(make_pair(0,0),0));
  13.     while (!pq.empty()) {
  14.         int node = pq.top().second;
  15.         int used = abs(pq.top().first.first);
  16.         int dist = abs(pq.top().first.second);
  17.         pq.pop();
  18.         for (pair<pair<int,int>,int> child : adj[node]) {
  19.             int child_cost = child.first.second;
  20.             int child_node = child.first.first;
  21.             int child_used = child.second;
  22.             if (child_used + used <= d and child_cost + dist < cost[child_node]) {
  23.                 cost[child_node] = child_cost + dist;
  24.                 pq.push(make_pair(make_pair(-(child_used + used),-cost[child_node]),child_node));
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30. int main () {
  31.      ios::sync_with_stdio(false);
  32.      cin.tie(nullptr);
  33.      cout.tie(nullptr);
  34.      int T;
  35.      cin >> T;
  36.      for (int test_case = 1; test_case <= T; ++test_case) {
  37.          int n,m,k,d;
  38.          cin >> n >> m >> k >> d;
  39.          for (int i = 0; i <= n; ++i) adj[i].clear();
  40.          for (int i = 1; i <= m; ++i) {
  41.              int u,v,cost;
  42.              cin >> u >> v >> cost;
  43.              adj[u].push_back(make_pair(make_pair(v,cost),0));
  44.          }
  45.          for (int i = 1; i <= k; ++i) {
  46.              int u,v,cost;
  47.              cin >> u >> v >> cost;
  48.              adj[u].push_back(make_pair(make_pair(v,cost),1));
  49.          }
  50.          dijkstra(0,n,d);
  51.          cout << "Case " << test_case << ": " << (cost[n - 1] >= (int) 1e7 ? "Impossible" : to_string(cost[n - 1])) << '\n';
  52.      }
  53.      //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement