uva10048(floyd)Audiophobia

题目连接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=989

题意:给出n个城镇,有m条路径,每条路径上会有噪音,给出两个城镇sta, end,问从sta到end的所有路径中受到的一段噪音最大值中最小的数。


例如:

如图:从1到7有 四条路线1367,1347,1257,1247,其中这四条路线中噪音最大值分别为120,80,90,120,则噪音最大值的最小值就是80;

思路:

最短路floyd算法,只要比较两条路径得到噪音的较大的选出最小的赋给g[i][j],输出g[sta][end]。

#include<bits/stdc++.h>
using namespace std;
const int N = 100;
int g[N][N];
int n,m,que;
const int INF = 0x3f3f3f3f;

void floyd() {
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                int maxx = g[i][k] < g[k][j] ? g[j][k]:g[i][k];  //以每一个k为转折点,找到这条路上噪音的最大值
                if(g[i][j] > maxx) {    //噪音最大值的最小值
                    g[i][j] = maxx;
                }
            }
        }
    }
}
int main()
{
    int t = 1;
    while(~scanf("%d%d%d",&n,&m,&que)&&n||m||que) {
        memset(g,INF,sizeof(g));  //初始化为无穷
        int a,b,c;
        for(int i = 0; i < m; i++) {
            cin>>a>>b>>c;
            g[a][b] = g[b][a] = c;
        }
        floyd();
        if(t != 1) {
            printf("\n");
        }
        printf("Case #%d\n",t++);
        int sta,end;
        while (que--) {
            scanf("%d%d", &sta, &end);
            if (g[sta][end] < INF) {   //若是无穷,则说明无解
                printf("%d\n", g[sta][end]);
            }
            else {
                printf("no path\n");
            }
        }
    }
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/clb123/p/11652582.html