Naxocist

Grab

Apr 29th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define INF 1e9
  5.  
  6. using ll = long long;
  7. using pi = pair<int, int>;
  8. using vi = vector<int>;
  9. using piv = pair<int, vi>;
  10.  
  11. const int N = 3e4 + 100;
  12.  
  13. int sff[50][N], dist[N], tmp[N];
  14. vector<pi> adj[N];
  15.  
  16. int main(){
  17.     ios_base::sync_with_stdio(false); cin.tie(nullptr);
  18.    
  19.     int n, m, k, q; cin >> n >> m >> k >> q;
  20.  
  21.     int u, v, w;
  22.     for(int i=0; i<m; ++i){
  23.         cin >> u >> v >> w;
  24.         adj[u].push_back({v, w});
  25.         adj[v].push_back({u, w});
  26.     }
  27.  
  28.     for(int i=0; i<=n; ++i) dist[i] = INF;
  29.     int idx = 0;
  30.     while(k--){
  31.         int shop; cin >> shop;
  32.         tmp[shop] = idx;
  33.  
  34.         priority_queue<pi, vector<pi>, greater<pi>> pq;
  35.         dist[shop] = 0;
  36.         pq.push({0, shop});
  37.         while(pq.size()){
  38.             pi t = pq.top(); pq.pop();
  39.             w = t.first, u = t.second;
  40.  
  41.             if(w > dist[u]) continue;
  42.  
  43.             for(pi e : adj[u]){
  44.                 v = e.first;
  45.                 int vw = e.second;
  46.                 if(w + vw < dist[v]){
  47.                     dist[v] = w + vw;
  48.                     pq.push({dist[v], v});
  49.                 }
  50.             }
  51.         }
  52.  
  53.         for(int i=0; i<=n; ++i) {
  54.             sff[idx][i] = dist[i];
  55.             dist[i] = INF;
  56.         }
  57.         idx++;
  58.  
  59.     }
  60.  
  61.  
  62.     while(q--){
  63.         cin >> u >> w >> v;
  64.         k = tmp[w];
  65.         cout << sff[k][u] + sff[k][v] << '\n';
  66.     }
  67.     return 0;
  68.  
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment