-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPFA.cpp
More file actions
79 lines (73 loc) · 1.89 KB
/
Copy pathSPFA.cpp
File metadata and controls
79 lines (73 loc) · 1.89 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*************************************************************************
> File Name: SPFA.cpp
> Author: zhangfb
> Mail: 1819067326
> Created Time: 三 4/21 07:43:43 2021
************************************************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
using namespace std;
struct node {
int e, dis, next ;
};
int n , m , s, cnt, ans[10000], head[10000], que_mark[10000];
node edge[1000];
char mark[10003][10003];
int main() {
memset(ans, 0x3F, sizeof(ans));
memset(head, -1, sizeof(head));
cin >> n >> m >> s;
for (int i =1 ; i <= m; i++) {
int a, b , c;
cin >> a >> b >> c;
if (mark[a][b]) {
int ind = head[a];
while (edge[ind].e != b) {
ind = edge[ind].next;
}
edge[ind].dis = min(c, edge[ind].dis);
} else {
mark[a][b] = 1;
edge[cnt].e = b;
edge[cnt].dis = c;
edge[cnt].next = head[a];
head[a] = cnt;
cnt++;
}
}
ans[s] = 0;
queue<int> que;
que.push(s);
que_mark[s] = 1;
while (!que.empty()) {
int t = que.front();
que.pop();
que_mark[t] = 0;
for (int i = head[t]; i != -1; i = edge[i].next) {
if (ans[edge[i].e] > ans[t] + edge[i].dis) {
ans[edge[i].e] = ans[t] + edge[i].dis;
if (!que_mark[edge[i].e]) {
que_mark[edge[i].e] = 1;
que.push(edge[i].e);
}
}
}
}
for (int i = 1; i <= n; i++) {
if (i != 1) {
cout << " ";
}
if (ans[i] != 0x3f3f3f3f) {
cout << ans[i];
} else {
cout << 0x7FFFFFFF;
}
}
return 0;
}