-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.cpp
58 lines (51 loc) · 882 Bytes
/
Dijkstra.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MaxN=1e5+100;
int n,m,AvEd[MaxN];
bool mark[MaxN];
vector < pair<int, int> > adj [MaxN];
void input ()
{
cin>> n>> m;
for (int i=1;i<=m;i++)
{
int v,u,w;
cin>> v>> u>> w;
adj[v].push_back(make_pair(u,w));
adj[u].push_back(make_pair(v,w));
}
}
void expand(int v)
{
mark[v]=true;
for (int i=0;i<adj[v].size();i++)
if(AvEd[v]+adj[v][i].second<AvEd[adj[v][i].first])
AvEd[adj[v][i].first]=AvEd[v]+adj[v][i].second;
}
int extract_min()
{
int ret=0;
for (int i=1;i<=n;i++)
if(mark [i]==0 && AvEd[i]<=AvEd[ret])
ret=i;
return ret;
}
void Dijkstra(int v)
{
fill(AvEd,AvEd+n+1,INT_MAX/2);
AvEd[v]=0;
for(int i=1;i<=n;i++)
expand(extract_min());
}
int main()
{
input();
int u;
cin>>u;
Dijkstra(u);
for (int i=1;i<=n;i++)
cout<<AvEd[i]<<" ";
return 0;
}