因为会输入多组查询,因此用Floyd算法进行预处理效率会比每次重新算的效率要高一些。
因为题目要求的是一条最大权值最小的路,因此Floyd算法中的松弛操作的右边应该从加法变为max()。
Run Time: 0.022s
#define UVa "LT11-5.10048.cpp" //Audiophobia
char fileIn[30] = UVa, fileOut[30] = UVa;
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
struct Edge {
int from, to, weight;
Edge(int u, int v, int w):from(u), to(v), weight(w) {}
};
//Global Variables. Reset upon Each Case!
const int maxn = 100 + 5, INF = 1<<25;
int C, S, Q;
vector<Edge> edges;
int decibels[maxn][maxn];
/
void floyd() {
for(int i = 1; i <= C; i ++) {
for(int j = 1; j <= C; j ++) {
decibels[i][j] = INF;
}
}
for(int i = 1; i <= C; i ++) decibels[i][i] = 0;
for(int i = 0; i < S; i ++) {
int u = edges[i].from, v = edges[i].to, w = edges[i].weight;
decibels[u][v] = decibels[v][u] = w;
}
for(int k = 1; k <= C; k ++) {
for(int i = 1; i <= C; i ++) {
for(int j = 1; j <= C; j ++) {
if(decibels[i][k] != INF && decibels[k][j] != INF) {
decibels[i][j] = min(decibels[i][j], max(decibels[i][k], decibels[k][j]));
}
}
}
}
}
int main() {
int T = 0;
while(scanf("%d%d%d", &C, &S, &Q) && C) {
if(T) printf("\n");
printf("Case #%d\n", ++T);
edges.clear();
int c1, c2, d;
for(int i = 0; i < S; i ++) {
scanf("%d%d%d", &c1, &c2, &d);
edges.push_back(Edge(c1, c2, d));
}
floyd();
for(int i = 0; i < Q; i ++) {
scanf("%d%d", &c1, &c2);
if(decibels[c1][c2] >= INF) printf("no path\n");
else printf("%d\n", decibels[c1][c2]);
}
}
return 0;
}