-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDijkstra algo.cpp
More file actions
77 lines (64 loc) · 1.38 KB
/
Dijkstra algo.cpp
File metadata and controls
77 lines (64 loc) · 1.38 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include<bits/stdc++.h>
#define ll long long int
#define f(i,a,b) for(int i=a;i<=b;i++)
#define g(i,a,b) for(int i=a;i>=b;i--)
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define INF 1000000000000000000
using namespace std;
vector<pair<ll,ll> > v[200001];
ll n,m;
ll dis[200001];
void dijkstra(ll src)
{
for(ll i=1;i<=n;i++) dis[i]=INF;
dis[src]=0;
set<pair<ll,ll> > st;
st.insert({0,src});
while(!st.empty())
{
pair<ll,ll> tmp=*(st.begin());
ll p=tmp.S;
st.erase(st.begin());
for(auto i:v[tmp.S])
{
ll u=i.F;
ll wt=i.S;
if(dis[u]>dis[p]+wt)
{
if(dis[u]!=INF)
{
st.erase(st.find({dis[u],u}));
}
dis[u]=dis[p]+wt;
st.insert({dis[u],u});
}
}
}
}
int main()
{
cin>>n>>m;
for(ll i=0;i<m;i++)
{
ll x,y,w;
cin>>x>>y>>w;
v[x].pb({y,w});
v[y].pb({x,w});
}
ll src;
cin>>src;
dijkstra(src);
for(ll i=1;i<=n;i++)
{
cout<<dis[i]<<" ";
}
cout<<endl;
}