-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.cpp
62 lines (54 loc) · 1.3 KB
/
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
59
60
61
62
#include<iostream>
#include<vector>
#include<climits>
#include<queue>
#include<utility>
#define N 100005
#define M 200005
using namespace std;
struct Edge{
int from, to, dist;
Edge(int u,int v, int w) : from(u), to(v), dist(w) {}
};
vector<Edge> edges;
vector<int> G[N];
int n,m,s;
int d[N];
bool vis[N];
void Dijkstra(){
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>> > pq;
for(int i = 1; i <= n; i++) {
d[i] = INT_MAX;
vis[i] = 0;
}
d[s] = 0;
pq.push(pair<int,int>(0, s));
while(!pq.empty()) {
pair<int,int> x = pq.top();pq.pop();
int u = x.second;
if(vis[u]) continue;
vis[u] = 1;
for(int i = 0; i < G[u].size(); i++) {
Edge e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist) {
d[e.to] = d[u] + e.dist;
pq.push(pair<int,int>(d[e.to], e.to));
}
}
}
}
int main() {
// freopen("in.txt", "r", stdin);
cin>>n>>m>>s;
for(int i = 1; i <= n; i++) G[i].clear();
edges.clear();
for(int i = 0; i < m; i++) {
int u,v,w;
cin>>u>>v>>w;
edges.push_back(Edge(u,v,w));
G[u].push_back(edges.size()-1);
}
Dijkstra();
for(int i = 1; i <= n; i++) cout<<d[i]<<' ';
return 0;
}