YEZAELP

TOI13: Budget Travelling

Jun 5th, 2020 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int INF=1e9;
  4. using pi=pair<int,int>;
  5. using pii=pair<int,pi>;
  6.  
  7. int n,m,x,y,z;
  8. vector <pi> g[100001];
  9. set <pi> way;
  10.  
  11. bool dijk1(){
  12.     vector <int> dis(n+1,INF);
  13.     vector <bool> visited(n+1,false);
  14.     priority_queue <pi,vector<pi>,greater<pi>> pq;
  15.     dis[x]=0;
  16.     pq.push({0,x});
  17.     while(pq.size()>0){
  18.         int u,d;
  19.         d=pq.top().first;
  20.         u=pq.top().second;
  21.         pq.pop();
  22.  
  23.         if(visited[u])
  24.             continue;
  25.         visited[u]=true;
  26.  
  27.         if(u==y) {
  28.             printf("%d %d 0",y,d);
  29.             return true;
  30.         }
  31.  
  32.         for(auto vw:g[u]){
  33.             int v,w;
  34.             v=vw.first;
  35.             w=vw.second;
  36.             if(!visited[v] and d+w<dis[v] and d+w<=z){
  37.                 dis[v]=d+w;
  38.                 pq.push({d+w,v});
  39.             }
  40.             else if(!visited[v] and d+w<dis[v] and d+w>z) {
  41.                 way.insert({u,dis[u]});
  42.             }
  43.         }
  44.     }
  45.     return false;
  46. }
  47. int dijk2(int u){
  48.     vector <int> dis(n+1,INF);
  49.     vector <bool> visited(n+1,false);
  50.     priority_queue <pi,vector<pi>,greater<pi>> pq;
  51.     dis[u]=0;
  52.     pq.push({dis[u],u});
  53.     while(pq.size()>0){
  54.         int d;
  55.         d=pq.top().first;
  56.         u=pq.top().second;
  57.         pq.pop();
  58.  
  59.         if(visited[u])
  60.             continue;
  61.         visited[u]=true;
  62.  
  63.         if(u==y){
  64.             return d;
  65.         }
  66.  
  67.         for(auto vw:g[u]){
  68.             int v,w;
  69.             v=vw.first;
  70.             w=vw.second;
  71.             if(!visited[v] and d+w<dis[v]){
  72.                 dis[v]=d+w;
  73.                 pq.push({d+w,v});
  74.             }
  75.         }
  76.  
  77.     }
  78. }
  79. int main(){
  80.  
  81.     scanf("%d%d%d%d%d",&n,&m,&x,&y,&z);
  82.  
  83.     for(int i=0;i<m;i++){
  84.         int u,v,w;
  85.         scanf("%d%d%d",&u,&v,&w);
  86.         g[u].push_back({v,w});
  87.         g[v].push_back({u,w});
  88.     }
  89.  
  90.     int node,mn=INF,dis;
  91.     if( !dijk1() ) {
  92.         for(auto w:way){
  93.             int D=dijk2(w.first);
  94.             if(D<mn){
  95.                 mn=D;
  96.                 node=w.first;
  97.                 dis=w.second;
  98.             }
  99.         }
  100.         printf("%d %d %d",node,dis,mn);
  101.     }
  102.  
  103.  
  104.     return 0;
  105. }
  106. /*
  107.  
  108. 8 11 0 5 200 0 1 10 0 2 10 1 3 10 1 5 70 2 3 10 2 4 30 2 6 10 3 5 30 4 5 20 6 7 15 7 5 20
  109.  
  110. 8 11 0 5 45 0 1 10 0 2 10 1 3 10 1 5 70 2 3 10 2 4 30 2 6 10 3 5 30 4 5 20 6 7 15 7 5 20
  111. */
Add Comment
Please, Sign In to add comment