-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeforces20C.cpp
63 lines (63 loc) · 1.36 KB
/
Codeforces20C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define RESET(a, b) memset(a, b, sizeof(a))
#define dis first
#define node second
#define pb push_back
#define pii pair<int,int>
const int mx = 100005;
const int inf = 0x3f3f3f3f;
int x[mx+2];
vector <pii> adj[mx];
int dis[mx];
void dijkastra(int start)
{
priority_queue<pii,vector<pii>,greater<pii>>q;
RESET(dis,inf);
RESET(x,-1);
q.push(pii(0,start));
dis[start]=0;
x[start]=inf;
while(!q.empty())
{
int n=q.top().node;
int d=q.top().dis;
q.pop();
if(d>dis[n]) continue;
for(int i=0;i<adj[n].size();i++)
{
int a=adj[n][i].node;
int b=adj[n][i].dis;
if(d+b<dis[a])
{
dis[a]=d+b;
x[a]=n;
q.push(pii(dis[a],a));
}
}
}
}
void printpath(int start,int end)
{
if(start==end) cout<<start<<" ";
else if(x[end]==-1) cout<<"-1";
else {
printpath(start,x[end]);
cout<<end<<" ";
}
}
main()
{
int node,edge,i,j,e1,e2,cost;
cin>>node>>edge;
for(i=0;i<edge;i++)
{
cin>>e1>>e2>>cost;
adj[e1].pb(pii(cost,e2));
adj[e2].pb(pii(cost,e1));
}
dijkastra(1);
printpath(1,node);
cout<<endl;
}