Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct DATA{
  5.     int i;
  6.     long long w;
  7.     long long maxx;
  8.  
  9.     bool operator <(const DATA & d2) const{
  10.         return w < d2.w;
  11.     }
  12. };
  13.  
  14. vector<pair<int,long long>>g[100100];
  15. priority_queue<DATA>pq;
  16.  
  17. long long dis[100100];
  18. pair<int,long long> from[100100];
  19. int main(){
  20.  
  21.     int n,m,u,v,s,t;
  22.     long long w,ans=0;
  23.     scanf(" %d %d",&n,&m);
  24.     scanf(" %d %d",&s,&t);
  25.     for(int i = 1;i<=m;i++){
  26.         scanf(" %d %d %lld",&u,&v,&w);
  27.         ans += w;
  28.         g[u].push_back({v,w});
  29.         g[v].push_back({u,w});
  30.     }
  31.  
  32.     for(int i = 0;i<=n;i++) dis[i] = 1e9;
  33.     dis[s] = 0;
  34.     pq.push({s,(long long)0,(long long)0});
  35.  
  36.     while(!pq.empty()){
  37.         DATA f = pq.top();
  38.         pq.pop();
  39.         //cout << f.i << " " << f.w << " " << f.maxx <<endl;
  40.         for(auto x : g[f.i]){
  41.             int to = x.first;
  42.             w = x.second;
  43.             //cout << "to " << to << " " << w << endl;
  44.             if(f.w + w - max(w,f.maxx) < dis[to]){
  45.                 dis[to] = f.w + w - max(w,f.maxx);
  46.                 pq.push({to,f.w + w,max(f.maxx,w)});
  47.                 //cout << "Push " << to << " " << dis[to] << " " << f.w + w - max(w,f.maxx) << endl;
  48.             }
  49.         }
  50.     }
  51.     //cout << dis[t] << endl;
  52.     printf("%lld",ans-dis[t]);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement