10048 - Audiophobia

点击打开链接



用Floyd 算法求任意两个点相通路径中的所有路径长度最长的值。只用FLOYD 和决策改变一下即可。


#include
int n,m,t;
const int INF = 100000000;
const int maxn = 110;
int dist[maxn][maxn];

int maxs(int a,int b){
    return a>b?a:b;
}

void floyd(){
    int i,j,k;
    for(k = 1;k<=n;k++){
        for(i = 1;i<=n;i++){
            for(j = 1;j<=n;j++) {
                if(dist[i][j] > maxs(dist[i][k],dist[k][j]))
                    dist[i][j] = maxs(dist[i][k],dist[k][j]);
            }
        }
    }
}

int main(){
    int i,j;
    int a,b;
    int cnt = 1;
    while(scanf("%d%d%d",&n,&m,&t)!=EOF,n+m+t){
        for(i = 1;i<=100;i++){
            for(j = 1;j<=100;j++){
                if(i == j) dist[i][j] = 0;
                else dist[i][j] = INF;
            }
        }
        for(i = 1;i<=m;i++){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            dist[u][v] = dist[v][u] = w;
        }
        floyd();
        if(cnt >=2) printf("\n");
        printf("Case #%d\n",cnt++);
        while(t--){
            scanf("%d%d",&a,&b);
            if(dist[a][b]!=INF) printf("%d\n",dist[a][b]);
            else printf("no path\n");
        }
    }
    return 0;
}


你可能感兴趣的:(图论)