AOJ 0189 Convenient Location 最短路 Floyd-Warshall算法
作者:互联网
题目大意:
求某一个办公室 到其他所有办公室的 总距离最短 办公室数不超过10(编号为0~9)
输入:多组输入,每组第一行为n (1 ≤ n ≤ 45),接下来n行是 (x, y, d),x到y的距离是d
输出:办公室号 和 总的最短距离
解题思路:
基础的Floyd-Warshall算法
AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
int path[10][10];
#define INF 0x3fffffff
void init() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
path[i][j] = -1;
}
path[i][i] = 0;
}
}
int main() {
int n;
while (cin >> n&&n) {
init();
int a, b; int V=0;//V代表办公室数量
for (int i = 0; i < n; i++) {
cin >> a >> b; cin >> path[a][b];
path[b][a] = path[a][b];
V = max(V, max(a, b) + 1);//注意这里要手动求出办公室数量
}
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (path[i][k] == -1 || path[k][j] == -1) continue;
if (path[i][j] == -1 || path[i][j] > path[i][k] + path[k][j]) {
path[i][j] = path[i][k] + path[k][j];
}
}
}
}
int ans = INF; int pos; int tmp;
for (int i = 0; i < V; i++) {
tmp = 0;
for (int j = 0; j < V; j++) {
tmp += path[i][j];
}
if (tmp < ans) {
ans = tmp; pos = i;
}
}
cout << pos << " " << ans << endl;
}
}
标签:tmp,10,办公室,Warshall,Convenient,0189,++,int,path 来源: https://blog.csdn.net/RPG_Zero/article/details/100009426