——by A Code Rabbit
Description
给你一张图。
求图上任意两点间,经过的路径中最大值最小的路径。
输入以边的方式表达的图。
输出所求路径上的最大值。
Types
Graph Algorithms
Analysis
询问任意两点间的最优路径,可以想到Floyd。
只要修改Floyd选边的策略就可以了。
// UVaOJ 10048
// Audiophobia
// by A Code Rabbit
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAXV = 102;
const int INF = 1e9;
template <typename T>
struct Graph {
T mat[MAXV][MAXV];
void Init(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
mat[i][j] = i == j ? 0 : INF;
}
}
}
void AddEdge(int u, int v, T w) {
mat[u][v] = w;
}
};
namespace Floyd {
template <typename T>
void Go(T w[MAXV][MAXV], int n) {
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (max(w[i][k], w[k][j]) < w[i][j])
w[i][j] = max(w[i][k], w[k][j]);
}
}
int c, s, q;
int c1, c2, d;
Graph<int> graph;
int main() {
int tot_case = 0;
while (scanf("%d%d%d", &c, &s, &q) && (c || s || q)) {
// Input.
graph.Init(c);
for (int i = 0; i < s; i++) {
scanf("%d%d%d", &c1, &c2, &d);
graph.AddEdge(c1 - 1, c2 - 1, d);
graph.AddEdge(c2 - 1, c1 - 1, d);
}
// Solve.
Floyd::Go(graph.mat, c);
// Output.
printf("%s", tot_case ? "\n" : "");
printf("Case #%d\n", ++tot_case);
for (int i = 0; i < q; i++) {
scanf("%d%d", &c1, &c2);
if (graph.mat[c1 - 1][c2 - 1] != INF)
printf("%d\n", graph.mat[c1 - 1][c2 - 1]);
else
printf("no path\n");
}
}
return 0;
}