Advertisement
Guest User

dsa

a guest
Apr 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,k,a,b,c;
  4. vector <int> v[100002];
  5. vector <int> w[100002];
  6. pair<int,int> f[100002];
  7. vector <int> l;
  8. int t[100002];
  9.  
  10.     int main() {
  11. cin>>n>>k;
  12. for(int i=0;k>i;i++)
  13. {
  14.     cin>>a>>b>>c;
  15.     v[a].push_back(b);
  16.     w[a].push_back(c);
  17.     v[b].push_back(a);
  18.     w[b].push_back(c);
  19. }
  20. priority_queue <pair<int,int >  ,vector <pair <int,int> > ,greater <pair<int,int > > >q;
  21. q.push({0,1});
  22. for(int r=2;n>=r;r++)
  23. {
  24.     f[r].first=100000000;
  25. }
  26. t[1]=0;
  27.  
  28. while(!q.empty())
  29. {
  30.     int x=q.top().first;
  31.     int y=q.top().second;
  32.  
  33.     q.pop();
  34.     if(t[y])
  35.         continue;
  36.  
  37.     t[y]=x;
  38.     for(int i=0;v[y].size()>i;i++)
  39.     {
  40.         if(t[v[y][i]]==0 and v[y][i]!=1 and t[y]+w[y][i]<f[v[y][i]].first)
  41.             {q.push({t[y]+w[y][i],v[y][i]});f[v[y][i]].first=t[y]+w[y][i];f[v[y][i]].second=y;}
  42.     }
  43. }
  44. l.push_back(n);
  45. int m=n;
  46. while(f[m].second!=0)
  47. {
  48.     l.push_back(f[m].second);
  49.     m=f[m].second;
  50. }
  51. sort(l.begin(),l.end());
  52. for(int i=0;l.size()>i;i++)
  53.     cout<<l[i]<<endl;
  54.  
  55.  
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement