-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
60 lines (53 loc) · 1.22 KB
/
dijkstra.cpp
File metadata and controls
60 lines (53 loc) · 1.22 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
#include<queue>
#include<iostream>
#include<vector>
using namespace std;
int inf = 1e9;
vector<pair<int,int>> ad[100005];
vector<int> dist(100005,inf);
vector<int> vis(100005,0);
void dijsktra(int source)
{
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > q;
q.push(make_pair(0, source));
dist[source]=0;
while(!q.empty())
{
int top_vtx = q.top().second;
q.pop();
if(vis[top_vtx]==1)
continue;
vis[top_vtx]=1;
for(int i=0;i<ad[top_vtx].size();i++)
{
int x = ad[top_vtx][i].first;
int w = ad[top_vtx][i].second;
if(dist[x] > dist[top_vtx] + w && vis[x]==0)
{
dist[x] = dist[top_vtx] + w;
q.push(make_pair(dist[x], x));
}
}
}
}
int main()
{
int n,m;
cin>>n>>m;
int i;
for(i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
ad[u].push_back(make_pair(v,w));
ad[v].push_back(make_pair(u,w));
}
int source;
cin>>source;
dijsktra(source);
for(i=0;i<n;i++)
{
cout<<"Shortest distance between "<<source<<" and "<<i<<" is "<<dist[i]<<endl;
}
return 0;
}