题意:
给出n条边和权值, 再给出若干对点, 求到达两点间经过的权值最大的边最小的那条路径中所有权值的和。
解题思路:
对于这样的问题最短路和生成树算法都可以解决, 但是因为多组查询所以使用Floyd算法比较合适。
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=989
Memory: 0 KB | Time: 3 MS | |
Language: C++11 4.8.2 | Result: Accepted |
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)
int buf[10];
inline long long read()
{
long long x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void writenum(int i)
{
int p = 0;
if(i == 0) p++;
else while(i)
{
buf[p++] = i % 10;
i /= 10;
}
for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 200
const int INF = 0x3f3f3f3f;
int G[MAX_N][MAX_N];
int main()
{
int cas = 1;
int n, m , q;
while(~scanf("%d%d%d", &n, &m, &q) && (n + m + q))
{
memset(G, INF, sizeof(G));
int u, v, w;
for(int i = 0 ; i < m ; i ++)
{
u = read();
v = read();
w = read();
G[u][v] = G[v][u] = w;
}
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], max(G[i][k], G[k][j]));
}
}
}
if(cas != 1) printf("\n");
printf("Case #%d\n", cas++);
for(int i = 0 ; i < q ; i++)
{
u = read();
v = read();
if(G[u][v] == INF)
printf("no path\n");
else
printf("%d\n", G[u][v]);
}
}
return 0;
}