UVA-10048 Audiophobia(最短路上的最小值,Folyd)

原有的Floyd算法应该是:

for(int k = 1;k <= N;k++)
            for(int p = 1;p <= N;p++)
                for(int q = 1;q <= N;q++)
                {
                    if(d[p][k] == INF || d[k][q] == INF)
                        continue;
                    d[p][q] = min(d[p][q],d[p][k] + d[k][q]));
                }

此处只需要把加号改成max即可

//#include 
//#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define INF 0x3f3f3f3f
#define eps 1e-8
typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;

int M,N,Q;

int d[1005][1005];

int main()
{
    int T = 0;
    //freopen("int.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d %d %d",&N,&M,&Q))
    {
        if(!N && !M && !Q)
            break;
        for(int i = 1;i <= N;i++)
            for(int j = 1;j <= N;j++)
                d[i][j] = (i == j) ? 0 : INF;
        int l,r,n;
        for(int i = 0;i < M;i++){
            scanf("%d %d %d",&l,&r,&n);
            d[l][r] = d[r][l] = n;
        }
        for(int k = 1;k <= N;k++)
            for(int p = 1;p <= N;p++)
                for(int q = 1;q <= N;q++)
                {
                    if(d[p][k] == INF || d[k][q] == INF)
                        continue;
                    d[p][q] = min(d[p][q],max(d[p][k],d[k][q]));
                }
        if(T)
            puts("");
        printf("Case #%d\n",++T);
        while(Q--)
        {
            int c1,c2;
            scanf("%d %d",&c1,&c2);
            if(d[c1][c2] == INF)
                puts("no path");
            else
                printf("%d\n",d[c1][c2]);
        }
    }
    return 0;
}

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