MAGCARI

God Of War

Nov 16th, 2022
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. /*
  2.     Task    : _example
  3.     Author  : Phumipat C. [MAGCARI]
  4.     Language: C++
  5.     Created : 16 November 2022 [21:15]
  6. */
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9. struct A{
  10.     int nowNode,sumDistance,skipped;
  11.     bool operator < (const A&o) const{
  12.         return sumDistance > o.sumDistance;
  13.     }
  14. };
  15. priority_queue<A > heap;
  16. struct B{
  17.     int nextNode,edgeWeight;
  18. };
  19. vector<B > g[100010];
  20. int dis[100010][2];
  21. int main(){
  22.     int n,m,s,t,u,v,w,sumWeight = 0;
  23.     scanf("%d %d %d %d",&n,&m,&s,&t);
  24.     for(int i=1;i<=m;i++){
  25.         scanf("%d %d %d",&u,&v,&w);
  26.         g[u].push_back({v,w});
  27.         g[v].push_back({u,w});
  28.         sumWeight+=w;
  29.     }
  30.     for(int i=0;i<n;i++)
  31.         dis[i][0] = dis[i][1] = 1e9;
  32.     dis[s][0] = 0;
  33.     heap.push({s,0,0});
  34.     while(!heap.empty()){
  35.         A now = heap.top();
  36.         heap.pop();
  37.         for(auto x:g[now.nowNode]){
  38.             //normal
  39.             if(dis[x.nextNode][now.skipped] > now.sumDistance + x.edgeWeight){
  40.                 dis[x.nextNode][now.skipped] = now.sumDistance + x.edgeWeight;
  41.                 heap.push({x.nextNode,now.sumDistance + x.edgeWeight,now.skipped});
  42.             }
  43.  
  44.             //special
  45.             if(now.skipped == 0 && dis[x.nextNode][now.skipped+1] > now.sumDistance){
  46.                 dis[x.nextNode][now.skipped+1] = now.sumDistance;
  47.                 heap.push({x.nextNode,now.sumDistance,now.skipped+1});
  48.             }
  49.         }
  50.     }
  51.     printf("%d\n",sumWeight - dis[t][1]);
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment