AhmedAshraff

Untitled

Jun 2nd, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define boAshraf ios_base::sync_with_stdio(false); cin.tie(NULL);
  4. #define ll long long
  5. #define sz(s) (int)(s).size()
  6. #define all(s) (s).begin(),(s).end()
  7. using namespace std;
  8.  
  9. void File();
  10.  
  11. void sol();
  12.  
  13. const int N = 1e5 + 5;
  14. int idx[N];
  15.  
  16. int main() {
  17.     boAshraf
  18.     File();
  19.     int t = 1;
  20.     cin >> t;
  21.     while (t--) {
  22.         sol();
  23.     }
  24.     return 0;
  25. }
  26.  
  27. void sol() {
  28.     int n, m;
  29.     cin >> n >> m;
  30.     vector<vector<pair<int, int>>> adj(n + 1);
  31.     vector<vector<pair<int, int>>> rev_adj(n + 1);
  32.     for (int i = 1; i <= m; i++) {
  33.         int u, v, w;
  34.         cin >> u >> v >> w;
  35.         adj[u].emplace_back(v, w);
  36.         rev_adj[v].emplace_back(u, w);
  37.     }
  38.     int k;
  39.     cin >> k;
  40.     vector<int> imp(k);
  41.     for (int i = 0; i < k; i++) {
  42.         cin >> imp[i];
  43.         idx[imp[i]] = i;
  44.     }
  45.     // from u to k[i]
  46.     vector<vector<ll>> dist_from(k, vector<ll>(n + 1, 1e15));
  47.     // from k[i] to u
  48.     vector<vector<ll>> dist_to(k, vector<ll>(n + 1, 1e15));
  49.     for (int i = 0; i < k; ++i) {
  50.         {
  51.             priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
  52.             dist_to[i][imp[i]] = 0;
  53.             pq.emplace(0, imp[i]);
  54.             while (!pq.empty()) {
  55.                 auto [cost, u] = pq.top();
  56.                 pq.pop();
  57.                 if (dist_to[i][u] != cost)continue;
  58.                 for (auto [v, w]: adj[u]) {
  59.                     if (dist_to[i][v] > cost + w) {
  60.                         dist_to[i][v] = cost + w;
  61.                         pq.emplace(dist_to[i][v], v);
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         {
  67.             priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
  68.             dist_from[i][imp[i]] = 0;
  69.             pq.emplace(0, imp[i]);
  70.             while (!pq.empty()) {
  71.                 auto [cost, u] = pq.top();
  72.                 pq.pop();
  73.                 if (dist_from[i][u] != cost)continue;
  74.                 for (auto [v, w]: rev_adj[u]) {
  75.                     if (dist_from[i][v] > cost + w) {
  76.                         dist_from[i][v] = cost + w;
  77.                         pq.emplace(dist_from[i][v], v);
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     vector<vector<vector<ll>>> dp(k, vector<vector<ll>>(k, vector<ll>(1 << k, -1)));
  84.     int tot = (1 << k) - 1;
  85.     auto rec = [&](auto &self, int last, int to, int mask)->ll {
  86.         if (mask == tot)return 0;
  87.         if (last == to)return 1e18;
  88.         ll &ret = dp[last][to][mask];
  89.         if (~ret)return ret;
  90.         ret = 1e15;
  91.         for (int i = 0; i < k; i++) {
  92.             if ((mask >> i) & 1)continue;
  93.             ret = min(ret, self(self, i, to, mask | (1 << i)) + dist_to[last][imp[i]]);
  94.         }
  95.         return ret;
  96.     };
  97.     int q;
  98.     cin >> q;
  99.     while (q--) {
  100.         int s, e;
  101.         cin >> s >> e;
  102.         ll ans = 1e15;
  103.         if (k == 1) {
  104.             ans = dist_from[0][s] + dist_to[0][e];
  105.         } else {
  106.             for (int i = 0; i < k; i++) {
  107.                 for (int j = 0; j < k; j++) {
  108.                     if(i==j)continue;
  109.                     ll c=dist_from[i][s]+dist_to[j][e]+rec(rec,i,j,1<<i);
  110.                     ans=min(ans,c);
  111.                 }
  112.             }
  113.         }
  114.         cout << ans << '\n';
  115.     }
  116. }
  117.  
  118. void File() {
  119. // #ifndef ONLINE_JUDGE
  120.     freopen("strongly.in", "r", stdin);
  121.     // freopen("output.txt", "w", stdout);
  122. // #endif
  123. }
Advertisement
Add Comment
Please, Sign In to add comment