UVA - 10048 Audiophobia(Floyd求路径上最大值的最小)

题目&分析:

 

思路:

Floyd变形(见上述紫书分析),根据题目要求对应的改变判断条件来解题。

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1000000000
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,ll> pii;
const int maxn = 120;
int c,s,q;
int mp[maxn][maxn];

void init(){
    for(int i=1; i<=c; i++){
        mp[i][i] = 0;
        for(int j=i+1; j<=c; j++){
            mp[j][i] = mp[i][j] = inf;
        }
    }
}


int main(){
    //FRE();
    int kase = 0;
    while(scanf("%d%d%d",&c,&s,&q) && c){
        init();
        for(int i=0; i<s; i++){
            int ss,t,d;
            scanf("%d%d%d",&ss,&t,&d);
            mp[ss][t] = d;
            mp[t][ss] = d;
        }

        for(int k=1; k<=c; k++){
            for(int i=1; i<=c; i++){
                for(int j=1; j<=c; j++){
                    if(mp[i][k]<inf && mp[k][j]<inf){
                        mp[i][j] = min(mp[i][j],max(mp[i][k],mp[k][j]));//mark!!
                    }
                }
            }
        }
        if(kase) printf("\n");
        printf("Case #%d\n",++kase);
        while(q--){
            int st,en;
            scanf("%d%d",&st,&en);
            if(mp[st][en]==inf){
                printf("no path\n");
            }else{
                printf("%d\n",mp[st][en]);
            }
        }
    }
    return 0;
}

 

posted @ 2019-02-15 16:14  sykline  阅读(242)  评论(0编辑  收藏  举报