Advertisement
Slayerfeed

TOI13 Buget Travelling

May 9th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. long long int inf=2e9;
  3. using namespace std;
  4. using pii = pair<int,int> ;
  5. int main(){
  6.     int n,m;
  7.  
  8.     cin >> n >> m;
  9.  
  10.     int x ,y ,z;
  11.     cin >> x >> y >> z;
  12.     int u1 ,v1 ,d1;
  13.     vector<pii> g[n];
  14.     for(int i=0;i<m;++i){
  15.         scanf("%d%d%d",&u1,&v1,&d1);
  16.         g[u1].push_back(make_pair(v1,d1));
  17.         g[v1].push_back(make_pair(u1,d1));
  18.     }
  19.     vector<int> dist(n,inf);
  20.  
  21.     vector<bool> visit(n,false);
  22.     priority_queue<pii, vector<pii>, greater<pii>> q;
  23.     dist[x]=0;
  24.     q.push({dist[x],x});
  25.  
  26.     while(!q.empty()){
  27.         long long int u = q.top().second , d = q.top().first;
  28.  
  29.         q.pop();
  30.  
  31.         if(visit[u]){
  32.             continue;
  33.         }
  34.         visit[u] = true;
  35.         for(auto c : g[u]){
  36.             long long int a = c.first;
  37.             long long int b = c.second;
  38.  
  39.             if(!visit[a]&&dist[u]+b < dist[a]){
  40.                 dist[a]=dist[u]+b;
  41.                 q.push({dist[a],a});
  42.             }
  43.         }
  44.     }
  45.  
  46.     if(dist[y]<=z){
  47.         cout << y << " " << dist[y] << " 0";
  48.     }
  49.     else{
  50.         vector<int> dist2(n,inf);
  51.  
  52.         vector<bool> visit2(n,false);
  53.         priority_queue<pii, vector<pii>, greater<pii>> q2;
  54.         dist2[y]=0;
  55.         q2.push({dist2[y],y});
  56.  
  57.         while(!q2.empty()){
  58.             long long int u2 = q2.top().second , d2 = q2.top().first;
  59.  
  60.             q2.pop();
  61.  
  62.             if(visit2[u2]){
  63.                 continue;
  64.             }
  65.             visit2[u2] = true;
  66.             for(auto c : g[u2]){
  67.                 long long int a = c.first;
  68.                 long long int b = c.second;
  69.  
  70.                 if(!visit2[a]&&dist2[u2]+b < dist2[a]){
  71.                     dist2[a]=dist2[u2]+b;
  72.                     q2.push({dist2[a],a});
  73.                 }
  74.             }
  75.         }
  76.         long long int r=2e9;
  77.         int t;
  78.         for(int i=0;i<n;++i){
  79.             if(i==x||i==y){
  80.                 continue;
  81.             }
  82.             if(dist[i]<=z&&dist2[i]<r){
  83.                 r=dist2[i];
  84.                 t=i;
  85.             }
  86.         }
  87.  
  88.         cout << t << " " << dist[t] << " " << dist2[t];
  89.     }
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement