Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef pair<int,int> ii;
- typedef vector<int> vi;
- #define mp make_pair
- #define pb push_back
- vi adj[100001];
- vi cost[100001];
- int dis[100001],n;
- int path[100001];
- int dijkastra(int source , int destination )
- {
- priority_queue< ii, vector <ii> ,greater<ii> > pq;
- for(int i = 1; i<=n; i++)
- dis[i] = INT_MAX;
- dis[source] = 0;
- pq.push(mp(0,source));
- while(!pq.empty())
- {
- ii temp = pq.top();
- pq.pop();
- int u = temp.second;
- if(u == destination)
- return dis[u];
- for(int i = 0; i<adj[u].size(); i++)
- {
- int v = adj[u][i];
- int c = cost[u][i];
- if(dis[u]+c < dis[v])
- {
- dis[v] = dis[u] + c;
- path[v] = u;
- pq.push(mp(dis[v],v));
- }
- }
- }
- return dis[destination];
- }
- void print_path(int des)
- {
- if(path[des]==-1)
- {
- cout<<des<<' ';
- return;
- }
- print_path(path[des]);
- cout<<des<<' ';
- }
- int main()
- {
- int adge,a,b,w;
- cin>>n>>adge;
- while(adge--)
- {
- cin>>a>>b>>w;
- adj[a].pb(b);
- adj[b].pb(a);
- cost[a].pb(w);
- cost[b].pb(w);
- }
- dijkastra(1,n);
- if(dijkastra(1,n)!= INT_MAX)
- {
- path[1]=-1;
- print_path(n);
- }
- else
- cout<<-1<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement