-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP4779 【模板】单源最短路径(标准版).cpp
77 lines (66 loc) · 1.5 KB
/
P4779 【模板】单源最短路径(标准版).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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;//arr size too small will cause RE(seg fault)
typedef unsigned long long ull;
struct Edge{
int from;
int to;
ull w;
};
struct Node{
int n;
ull w;
friend bool operator < (const Node& a, const Node& b) {
return a.w>b.w;
}
};
int n, m, s = 1;
ull dis[maxn+1];
vector<Edge> edges;
vector<int > head[maxn];
void addEdge(int from, int to, ull w) {
edges.push_back((Edge){from, to, w});
head[from].push_back(edges.size()-1);
}
priority_queue<Node> pq;
void dijsktra(int start) {
for(int i=1; i<=n; i++) {
dis[i] = 1e10;
}
dis[start] = 0;
pq.push((Node){start, 0});
while(!pq.empty()) {
Node temp = pq.top();pq.pop();
int n = temp.n;
ull w = temp.w;
if(w!=dis[n]) {
continue;
}
for(int i=0; i<head[n].size(); i++) {
Edge& now = edges[head[n][i]];
if(dis[now.to]>dis[now.from]+now.w) {
dis[now.to] = dis[now.from] + now.w;
pq.push((Node){now.to, dis[now.to]});//it should update only at the time it needed
}
}
}
}
void print_ans() {
for(int i=1; i<=n; i++) {
cout<<dis[i]<<" ";
}
cout<<endl;
}
int main() {
cin>>n>>m>>s;
int from_, to_;
ull w;
for(int i=0; i<m; i++) {
cin>>from_>>to_>>w;
addEdge(from_, to_, w);
//addEdge(to_, from_, w);
}
dijsktra(s);
print_ans();
return 0;
}