UVA 10048(Floyd变形)

题意:

从a点到b点, 找到一条路径,使得这条路径上的所有噪音中最大的值是所有路径中最小的, 这个噪音值便是要求的。

思路:

Floyd改改就好,核心句改成取两个边的最大值,再更新取到最小的g[i][j]。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=105;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int n,m,q,g[maxn][maxn];
void floyd()
{
    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]));
            }
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    int cas=0,f=0;
    while(cin>>n>>m>>q)
    {
        if(!n&&!m&&!q)
            break;
        if(f)
        {
            cout<<endl;
        }
        f=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j)
                    g[i][j]=0;
                else
                    g[i][j]=inf;
            }
        }
        for(int i=0;i<m;i++)
        {
            int a,b,c;
            cin>>a>>b>>c;
            g[a][b]=c;
            g[b][a]=c;
        }
        floyd();
        cout<<"Case #"<<++cas<<endl;
        for(int i=0;i<q;i++)
        {
            int a,b;
            cin>>a>>b;
            if(g[a][b]==inf)
                cout<<"no path"<<endl;
            else
                cout<<g[a][b]<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dilly__dally/article/details/82625528