Advertisement
paulomiranda98

Vacation Plans

Nov 5th, 2020
2,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. const int MAXN = 50;
  3.  
  4. using namespace std;
  5. typedef long long ll;
  6. typedef pair<int, ll> pil;
  7. vector<pil> adj[3][MAXN];
  8. ll dist[MAXN][MAXN][MAXN][3];
  9. int p, n, m;
  10. typedef tuple<int, int, int, int, int> tp;
  11.  
  12. ll djikstra(int e1, int e2, int e3){
  13.   memset(dist, 0x3f, sizeof(dist));
  14.   priority_queue<tp, vector<tp>, greater<tp>> pq;
  15.   dist[0][0][0][0] = 0;
  16.   pq.emplace(0, 0, 0, 0, 0);
  17.   while(!pq.empty()){
  18.     auto [d1, a, b, c, i] = pq.top(); pq.pop();
  19.     if(d1 > dist[a][b][c][i]) continue;
  20.     if(i == 0){
  21.       for(auto [to, w]: adj[i][a]){
  22.         ll d = dist[a][b][c][i] + w;
  23.         if(d < dist[to][b][c][1]){
  24.           dist[to][b][c][1] = d;
  25.           pq.emplace(d, to, b, c, 1);
  26.         }
  27.       }
  28.     }else if(i == 1){
  29.       for(auto [to, w]: adj[i][b]){
  30.         ll d = dist[a][b][c][i] + w;
  31.         if(d < dist[a][to][c][2]){
  32.           dist[a][to][c][2] = d;
  33.           pq.emplace(d, a, to, c, 2);
  34.         }
  35.       }
  36.     }else{
  37.       for(auto [to, w]: adj[i][c]){
  38.         ll d = dist[a][b][c][i] + w;
  39.         if(d < dist[a][b][to][0]){
  40.           dist[a][b][to][0] = d;
  41.           pq.emplace(d, a, b, to, 0);
  42.         }
  43.       }
  44.     }
  45.   }
  46.   return dist[e1][e2][e3][0];
  47. }
  48. int main (){
  49.  
  50.   cin.tie(0);
  51.   cin.sync_with_stdio(0);
  52.  
  53.   cin >> p;
  54.   vector<int> obj(3, 0);
  55.   for(int k = 0; k < p; k++){
  56.     cin >> n >> m;
  57.     for(int i = 0; i < n; i++){
  58.       int w;
  59.       cin >> w;
  60.       adj[k][i].emplace_back(i, w);
  61.     }
  62.     for(int i = 0; i < m; i++){
  63.       int u, v;
  64.       ll w;
  65.       cin >> u >> v >> w;
  66.       u--; v--;
  67.       adj[k][u].emplace_back(v, w);
  68.     }
  69.    
  70.     cin >> obj[k];
  71.     obj[k]--;
  72.   }
  73.   for(int k = p; k < 3; k++){
  74.     adj[k][0].emplace_back(0, 0);
  75.   }
  76.   cout << djikstra(obj[0], obj[1], obj[2]) << endl;
  77.   return 0;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement