UVA10048Audiophobia(Floyd)

<span style="font-family:FangSong_GB2312;font-size:18px;">/*Sample Input
7 9 3
1 2 50
1 3 60
2 4 120
2 5 90
3 6 50
4 6 80
4 7 70
5 7 40
6 7 140
1 7
2 6
6 2
7 6 3
1 2 50
1 3 60
2 4 120
3 6 50
4 6 80
5 7 40
7 5
1 7
2 4
0 0 0
Sample Output
Case #1
80
60
60
Case #2
40
no path
80
Floyd
题意:n个点组成的一个图,求从点x到点y能选择的任意路径,通过时在这条路径上能听到的最小分贝值。
题目中需要判断的有多组,涉及每个点的最短路,所以更适合用floyd算法。
*/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
const int INF = 0x3f3f3f3f;
using namespace std;

int dist[1010][1010];
int n,m,k;
void Floyd()
{
    for(int t = 1; t <= n; t ++)
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                ///同一条路径取最大,不同路径取最小
                dist[i][j] = min(dist[i][j],max(dist[i][t],dist[t][j]));
}
int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int Case = 0,x,y,z;
    while(~scanf("%d%d%d",&n,&k,&m) && n&&m&&k)
    {
        ///初始化
        memset(dist,0,sizeof(dist));
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                dist[i][j] = (i == j) ? 0 : INF;
        for(int i = 0; i < k; i ++)
        {
            scanf("%d%d%d",&x,&y,&z);
            dist[x][y] = dist[y][x] = z;
        }
        Floyd();
        if(Case)
            printf("\n");
        printf("Case #%d\n",++ Case);
        for(int i = 0;i < m;i ++)
        {
            scanf("%d%d",&x,&y);
            if(dist[x][y] == INF)
                printf("no path\n");
            else
                printf("%d\n",dist[x][y]);
        }
    }
    return 0;
}</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值