标签:cat bsp i++ using 最短路 cstring lan define cst
https://vjudge.net/problem/Aizu-0189
题意:求某一点到其他所有点的最短路径之和,输出该点与和。
注意Floyd可以求多源最短路径,而Dijkstra只能求单源。
1 #include<cstdio> 2 #include<cmath> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstring> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #define LL long long 10 #define maxn 100005 11 #define MOD 1e9+7 12 #define INF 0x3f3f3f3f 13 typedef long long ll; 14 using namespace std; 15 int n, D[110][110], x, y, z, maxm, mini; 16 int main() 17 { 18 while(cin >> n, n){ 19 maxm = -INF; 20 for(int i = 0; i < 10; i++){ 21 for(int j = 0; j < 10; j++){ 22 D[i][j] = INF; 23 } 24 } 25 for(int i = 0; i < n; i++){ 26 cin >> x >> y >> z; 27 D[x][y] = min(D[x][y], z); 28 D[y][x] = D[x][y]; 29 maxm = max(maxm, max(x, y)); 30 } 31 maxm++; 32 for(int i = 0; i < 10; i++){ 33 D[i][i] = 0; 34 } 35 for(int k = 0; k < maxm; k++){ 36 for(int i = 0; i < maxm; i++){ 37 for(int j = 0; j < maxm; j++){ 38 if(D[i][j] > D[i][k]+D[k][j]){ 39 D[i][j] = D[i][k]+D[k][j]; 40 } 41 } 42 } 43 } 44 mini = INF; 45 int loc = -1; 46 for(int i = 0; i < maxm; i++){ 47 int sum = 0; 48 for(int j = 0; j < maxm; j++){ 49 sum += D[i][j]; 50 } 51 if(mini > sum){ 52 mini = sum; 53 loc = i; 54 } 55 } 56 cout << loc << " " << mini << endl; 57 } 58 return 0; 59 }
Aizu0189 Convenient Location(多源最短路)
标签:cat bsp i++ using 最短路 cstring lan define cst
原文地址:https://www.cnblogs.com/Surprisezang/p/8983271.html