Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<long,long> pl;
  7. typedef pair<ll,ll> pll;
  8. typedef vector<long> vl;
  9. typedef vector<bool> vb;
  10. typedef vector<ll> vll;
  11. typedef vector<vl> vvl;
  12. typedef vector<vb> vvb;
  13. typedef vector<vll> vvll;
  14. typedef vector<pll> vpll;
  15. typedef vector<string> vs;
  16.  
  17. #define FOR(i,a,b) for(long long i=a;i<b;++i)
  18. #define REV(i,a,b) for(long long i=a;i>=b;i--)
  19. #define F first
  20. #define S second
  21. #define pb push_back
  22. #define mp make_pair
  23. #define all(v) v.begin(),v.end()
  24. #define tc ll t;cin>>t;while(t--)
  25. #define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  26. #define coutv(v) for(auto it: (v))cout<<it<<" ";newl;
  27. #define cout2d(v) for(auto it: (v)) {for(auto j:it) cout<<j<<" ";newl;}
  28. #define cinv(v,n) vll (v)(n);FOR(i,0,(n)){cin>>v[i];}
  29. #define cinvg(v,n) (v).resize(n);FOR(i,0,(n)){cin>>v[i];}
  30. #define cin2d(v,n,m) vvll (v)(n,vll(m,0));FOR(i,0,n){FOR(j,0,m){cin>>v[i][j];}}
  31. #define cin2dg(v,n,m) (v).resize(n,vll(m));FOR(i,0,n){FOR(j,0,m){cin>>v[i][j];}}
  32. #define newl cout<<"\n"
  33. #define mod 1000000007
  34. #define INF 100000000009
  35.  
  36. ll n,m;
  37. vector< vector<pair<ll,ll> > >g;
  38. vector<ll> dist,par;//out
  39. //vector<bool> vis(n);//out
  40.  
  41. void dijk(ll p);
  42.  
  43. int main()
  44. {
  45.     io;
  46.     ll a,b,w;
  47.     cin>>n>>m;
  48.     g.resize(n+1);
  49.     par.resize(n+1,-1);
  50.     par[1]=1;
  51.     dist.resize(n+1,INF);
  52.    
  53.     FOR(i,0,m)
  54.     {
  55.         cin>>a>>b>>w;
  56.         if(a==b){continue;}
  57.         g[a].pb({b,w});
  58.         g[b].pb({a,w});
  59.     }
  60.  
  61.     if(par[n]==-1){cout<<"-1";}
  62.     else
  63.     {
  64.         vll ans;
  65.         ans.pb(n);
  66.         ll x=n;
  67.         while(x!=1)
  68.         {
  69.             x=par[x];
  70.             ans.pb(x);
  71.         }
  72.         reverse(all(ans));
  73.         coutv(ans);
  74.     }
  75.  
  76.     return 0;
  77. }
  78. void dijk(ll p)
  79. {
  80.     priority_queue< pair<ll,ll> > q;
  81.     dist[p]=0;
  82.     q.push({0,p});
  83.     while(!q.empty())
  84.     {
  85.         ll a=q.top().S;
  86.         q.pop();
  87.         /*if(vis[a]){continue;}
  88.         vis[a]=true;*/
  89.  
  90.         for(auto v: g[a])
  91.         {
  92.             ll b=v.F,w=v.S;
  93.             if(dist[a]+w<dist[b])
  94.             {
  95.                 dist[b]=dist[a]+w;
  96.                 par[b]=a;
  97.                 q.push({-dist[b],b});
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement