-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.cpp
56 lines (50 loc) · 1.38 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
#include <bits/stdc++.h>
#define oo 1000007
using namespace std;
int n, m, s, t, went[101], d[101], goback[101];
vector <pair <int, int> > q[101];
void dijstra(int k){
memset(d, oo, sizeof d);
d[s] = 0;
while(true == true){
int u = 0;
for (int i = 1; i <= n; ++i){
if (!went[i] && d[i] < d[u]) u = i;
}
if (u == 0 || u == t) break;
went[u] = true;
while (!q[u].empty()){
if (d[q[u][0].first] > d[u] + q[u][0].second){
d[q[u][0].first] = d[u] + q[u][0].second;
goback[q[u][0].first] = u;
}
q[u].erase(q[u].begin());
}
}
}
void truy_vet(int k){
vector <int> temp;
while (k != 0){
temp.push_back(k);
k = goback[k];
}
for (int i = temp.size() - 1; i >= 1; --i){
printf("%d -> ", temp[i]);
}
printf("%d", temp[0]);
}
int main(){
freopen("dijkstra.inp", "r", stdin);
freopen("dijkstra.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &s, &t);
for (int i = 0; i < m; ++i){
int temp1, temp2, temp3;
scanf("%d%d%d", &temp1, &temp2, &temp3);
q[temp1].push_back(make_pair(temp2, temp3));
q[temp2].push_back(make_pair(temp1, temp3));
}
dijstra(s);
printf("%d\n", d[t]);
truy_vet(t);
return 0;
}