思路:直接Dijkstra,求源点1到其他各点的最短距离的最大值。
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 100 + 5;
int d[maxn], vis[maxn], w[maxn][maxn];
int n;
int dijkstra(int s) {
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; ++i) d[i] = inf;
d[s] = 0;
for(int i = 0; i < n; ++i) {
int x, m = inf;
for(int y = 1; y <= n; ++y) if(!vis[y] && d[y] < m) m = d[x=y];
vis[x] = 1;
for(int y = 1; y <= n; ++y) if(w[x][y] < inf) d[y] = min(d[y], d[x] + w[x][y]);
}
int ans = 0;
for(int i = 1; i <= n; ++i) {
ans = max(ans, d[i]);
}
return ans;
}
int main() {
while(scanf("%d", &n) == 1) {
for(int i = 1; i <= n; ++i)
for(int j = 1; j < n; ++j) {
w[i][j] = inf;
}
char s[50];
for(int i = 1; i <= n; ++i) {
for(int j = 1; j < i; ++j) {
scanf("%s", s);
if(s[0] != 'x') {
int dis;
sscanf(s, "%d", &dis);
w[i][j] = w[j][i] = dis;
}
}
}
printf("%d\n", dijkstra(1));
}
return 0;
}
如有不当之处欢迎指出!