MAGCARI

Royal Parade

Nov 21st, 2022 (edited)
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 21 November 2022 [21:41]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. struct A{
  10.     int node,weight;
  11. };
  12. vector<A > g[100010];
  13. struct B{
  14.     int currNode;
  15.     long long sumWeight;
  16.     bool operator < (const B&o) const{
  17.         return sumWeight > o.sumWeight;
  18.     }
  19. };
  20. priority_queue<B > heap;
  21. long long disA[100010],disB[100010],disC[100010];
  22. bool path[100010];
  23. int main(){
  24.     cin.tie(0)->sync_with_stdio(0);
  25.     cin.exceptions(cin.failbit);
  26.     int n,m,u,v,w,a,b,c,d;
  27.     cin >> n >> m;
  28.     for(int i=1;i<=m;i++){
  29.         cin >> u >> v >> w;
  30.         g[u].push_back({v,w});
  31.         g[v].push_back({u,w});
  32.     }
  33.     cin >> a >> b >> c >> d;
  34.     for(int i=1;i<=n;i++)
  35.         disA[i] = disB[i] = disC[i] = 1e18;
  36.     disA[a] = 0;
  37.     heap.push({a,0});
  38.     while(!heap.empty()){
  39.         B now = heap.top();
  40.         heap.pop();
  41.         for(auto x:g[now.currNode]){
  42.             if(disA[x.node] > now.sumWeight + x.weight){
  43.                 disA[x.node] = now.sumWeight + x.weight;
  44.                 heap.push({x.node,disA[x.node]});
  45.             }
  46.         }
  47.     }
  48.  
  49.     disB[b] = 0;
  50.     heap.push({b,0});
  51.     while(!heap.empty()){
  52.         B now = heap.top();
  53.         heap.pop();
  54.         for(auto x:g[now.currNode]){
  55.             if(disB[x.node] > now.sumWeight + x.weight){
  56.                 disB[x.node] = now.sumWeight + x.weight;
  57.                 heap.push({x.node,disB[x.node]});
  58.             }
  59.         }
  60.     }
  61.  
  62.     long long shortest = disA[b];
  63.  
  64.     for(int i=1;i<=n;i++)
  65.         if(disA[i] + disB[i] == shortest)
  66.             path[i] = true;
  67.  
  68.     if(!path[c]){
  69.         disC[c] = 0;
  70.         heap.push({c,0});
  71.     }
  72.     while(!heap.empty()){
  73.         B now = heap.top();
  74.         heap.pop();
  75.         for(auto x:g[now.currNode]){
  76.             if(disC[x.node] > now.sumWeight + x.weight && !path[x.node]){
  77.                 disC[x.node] = now.sumWeight + x.weight;
  78.                 heap.push({x.node,disC[x.node]});
  79.             }
  80.         }
  81.     }
  82.     if(disC[d] == 1e18) cout << "-1\n";
  83.     else                cout << disC[d] << '\n';
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment