-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.cpp
More file actions
63 lines (51 loc) · 965 Bytes
/
Copy pathDijkstra.cpp
File metadata and controls
63 lines (51 loc) · 965 Bytes
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
#include <iostream>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 1e6;
int path[1005][1005];
int d[1005];
bool vis[1005];
int n, m, s, t;
int main()
{
int a, b, c, pos;
scanf("%d %d %d %d", &n, &m, &s, &t);
for(int i = 1; i <= n; i ++)
{
fill(path[i], path[i] + n + 1, MAXN);
path[i][i] = 0;
d[i] = MAXN;
}
for(int i = 0; i < m; i ++)
{
scanf("%d %d %d", &a, &b, &c);
path[a][b] = min(path[a][b], c);
path[b][a] = min(path[b][a], c);
if(a == s) d[b] = min(d[b], c);
else if(b == s) d[a] = min(d[a], c);
}
memset(vis, 0, sizeof(vis));
d[s] = 0;
vis[s] = 1;
while(true)
{
pos = -1;
for(int i = 1; i <= n; i++)
{
if(vis[i]) continue;
if(pos == -1 || d[pos] > d[i])
pos = i;
}
if(pos == -1) break;
vis[pos] = true;
for(int i = 1; i <= n; i++)
{
d[i] = min(d[i], d[pos] + path[pos][i]);
}
}
printf("%d\n", d[t]);
return 0;
}