题目重述:
输出一个点,该点到其他所有点的距离的最大值最小,并输出最小的最大值。如果有任意两点无法到达,则输出0。(边权非负)
思路:
用FLoyd求出任意两点间距离的最小值。循环所有点,存储该节点到其他点的最大值maxl。最小的maxl值即为所求。若任意两点间距离无穷大,则不符合要求。
#include<iostream>
#include<cstring>
using namespace std;
const int N = 220, inf = 0x3f3f3f3f;
int g[N][N];
int n, m;
int main() {
cin >> n >> m;
memset(g, 0x3f, sizeof g);
cout << sizeof g << endl;
for(int i = 1; i <= n; ++ i) {
g[i][i] = 0;
}
while(m --) {
int a, b, c;
cin >> a >> b >> c;
g[a][b] = g[b][a] = min(g[a][b], c);
}
//先用Floyd求任意两点的最短路
for(int k = 1; k <= n; ++ k) {
for(int i = 1; i <= n; ++ i) {
for(int j = 1; j <= n; ++ j) {
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
//cout << g[i][j] << endl;
}
}
}
//找出可以与其他点均相通的点和最短的最长魔咒
int ans = inf, pos = -1;
bool flag = true;
for(int i = 1; i <= n; ++ i) {
int maxl = 0;
for(int j = 1; j <= n; ++ j) {
//
if(g[i][j] == inf) {
flag = false;
break;
}
maxl = max(maxl, g[i][j]);
}
//if(!flag) continue;
if(ans > maxl) {
ans = maxl;
pos = i;
//printf("%d %d\n", ans, pos);
}
}
if(!flag) cout << "0" << endl;
else cout << pos << ' ' << ans << endl;
return 0;
}