MAGCARI

Royal Parade

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