-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.cpp
61 lines (54 loc) · 1.29 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
#include <bits/stdc++.h>
#define re register
using namespace std;
inline int read() {
int X=0,w=1; char c=getchar();
while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
return X*w;
}
struct Edge { int v,w,nxt; };
Edge e[500010];
int head[100010],cnt=0;
inline void addEdge(int u,int v,int w) {
e[++cnt].v=v;
e[cnt].w=w;
e[cnt].nxt=head[u];
head[u]=cnt;
}
int n,m,s;
int dis[100010];
struct node { //堆节点
int u,d;
bool operator <(const node& rhs) const {
return d>rhs.d;
}
};
inline void Dijkstra() {
for (re int i=1;i<=n;i++) dis[i]=2147483647;
dis[s]=0;
priority_queue<node> Q; //堆
Q.push((node){s,0});
while (!Q.empty()) {
node fr=Q.top(); Q.pop();
int u=fr.u,d=fr.d;
if (d!=dis[u]) continue;
for (re int i=head[u];i;i=e[i].nxt) {
int v=e[i].v,w=e[i].w;
if (dis[u]+w<dis[v]) {
dis[v]=dis[u]+w;
Q.push((node){v,dis[v]});
}
}
}
}
int main() {
n=read(),m=read(),s=read();
for (re int i=1;i<=m;i++) {
int X=read(),Y=read(),Z=read();
addEdge(X,Y,Z);
}
Dijkstra();
for (re int i=1;i<=n;i++) printf("%d ",dis[i]);
return 0;
}