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
80
81
82
83
| #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
ll d[105][105];
int to[105][105];
void warshall_floyd(int n, int m) {
rep(i, n) rep(j, n) d[i][j] = INF;
rep(i, n) d[i][i] = 0;
rep(i, n) rep(j, n) to[i][j] = j;
//input
rep(i, m) {
int a, b, c;
cin >> a >> b >> c;
a--; b--;
d[a][b] = c;
// d[b][a] = c;
}
rep(k,n) {
rep(i,n) {
rep(j,n) {
if(d[i][k] == INF || d[k][j] == INF) continue;
if(d[i][j] > d[i][k] + d[k][j]) {
d[i][j] = d[i][k] + d[k][j];
to[i][j] = to[i][k];
}
}
}
}
}
vector<int> path(int s, int g) {
int cur = s;
vector<int> ret;
for(; cur != g; cur = to[cur][g]) {
ret.push_back(cur);
}
ret.push_back(g);
return ret;
}
int main() {
int n, m, s, g1, g2;
while(cin >> n >> m >> s >> g1 >> g2) {
if(n == 0 && m == 0 && s == 0 && g1 == 0 && g2 == 0) break;
s--; g1--; g2--;
warshall_floyd(n, m);
ll ans = INF;
rep(i, n) {
ans = min(ans, d[s][i] + d[i][g1] + d[i][g2]);
}
ans = min(ans, d[s][g1] + d[g1][g2]);
ans = min(ans, d[s][g2] + d[g2][g1]);
cout << ans << endl;
}
return 0;
}
|