大意:给你一个图,以及出发点与起始点,让你求这两者之间的最小的最大权值。
思路:动态规划,Floyd的变形,我理解起来比较困难,关键是我接触动态规划较少。
CODE:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
#define MAXN 110
#define INF 0X3F3F3F3F
int d[MAXN+10][MAXN+10];
int n, m, q;
void init()
{
for(int i = 1; i <= MAXN; i++)
{
for(int j = 1; j <= MAXN; j++)
{
if(i == j) d[i][j] = 0;
else d[i][j] = INF;
}
}
}
void Floyd()
{
int i, j, k;
for(k = 1; k <= n; k++)
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
d[i][j] = min(d[i][j], max(d[i][k], d[k][j]));
}
int main()
{
int times = 0;
while(scanf("%d%d%d", &n, &m, &q) && ( n || m || q))
{
init();
while(m--)
{
int u, v, cost;
scanf("%d%d%d", &u, &v, &cost);
d[u][v] = d[v][u] = cost;
}
Floyd();
if(times) printf("\n");
printf("Case #%d\n", ++times);
while(q--)
{
int u, v;
scanf("%d%d", &u, &v);
int ans = d[u][v];
if(ans == INF)
{
printf("no path\n");
}
else
{
printf("%d\n", ans);
}
}
}
}
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
#define MAXN 110
#define INF 0X3F3F3F3F
int d[MAXN+10][MAXN+10];
int n, m, q;
void init()
{
for(int i = 1; i <= MAXN; i++)
{
for(int j = 1; j <= MAXN; j++)
{
if(i == j) d[i][j] = 0;
else d[i][j] = INF;
}
}
}
void Floyd()
{
int i, j, k;
for(k = 1; k <= n; k++)
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
d[i][j] = min(d[i][j], max(d[i][k], d[k][j]));
}
int main()
{
int times = 0;
while(scanf("%d%d%d", &n, &m, &q) && ( n || m || q))
{
init();
while(m--)
{
int u, v, cost;
scanf("%d%d%d", &u, &v, &cost);
d[u][v] = d[v][u] = cost;
}
Floyd();
if(times) printf("\n");
printf("Case #%d\n", ++times);
while(q--)
{
int u, v;
scanf("%d%d", &u, &v);
int ans = d[u][v];
if(ans == INF)
{
printf("no path\n");
}
else
{
printf("%d\n", ans);
}
}
}
}