Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Problem: http://codeforces.com/problemset/problem/20/C
- #include<bits/stdc++.h>
- #define inf 1000000000000
- #define ll long long
- using namespace std;
- int n,vis[100005],par[100005];
- ll dist[100005];
- vector<int>adj[100005];
- vector<ll>cost[100005];
- struct node
- {
- int u;
- ll w;
- node(int x,ll z)
- {
- u=x;
- w=z;
- }
- };
- bool operator <(node a,node b)
- {
- return a.w>b.w;
- }
- priority_queue<node>pq;
- stack<int>s;
- void dijkstra()
- {
- int i;
- for(i=2;i<=n;i++)
- dist[i]=inf,par[i]=i;
- pq.push(node(1,0));
- while(!pq.empty())
- {
- node x=pq.top();
- pq.pop();
- if(x.w!=dist[x.u])
- continue;
- for(i=0;i<adj[x.u].size();i++)
- {
- int y=adj[x.u][i];
- if(dist[y]>dist[x.u]+cost[x.u][i])
- dist[y]=dist[x.u]+cost[x.u][i],pq.push(node(y,dist[y])),par[y]=x.u;
- }
- }
- }
- int main()
- {
- int i,m,x,y;
- ll z;
- scanf("%d%d",&n,&m);
- while(m--)
- {
- scanf("%d%d%lld",&x,&y,&z);
- adj[x].push_back(y);
- adj[y].push_back(x);
- cost[x].push_back(z);
- cost[y].push_back(z);
- }
- dijkstra();
- if(dist[n]==inf)
- printf("-1\n");
- else
- {
- par[1]=-1;
- x=n;
- while(x!=-1)
- {
- s.push(x);
- x=par[x];
- }
- while(!s.empty())
- printf("%d ",s.top()),s.pop();
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement