POJ3615-Floyd

http://poj.org/problem?id=3615

因为只需要求所在路径的最大高度的最小值,而且n<=300,我们可以用floyd跑。

g[i][j]=min(g[i][j],max(g[i][k],g[k][j]),简单地比大小,求最大值的最小值。

注意要先将g设为无限大。

#include<iostream>
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<cstring>
using namespace std;
inline int read()
{
    int w=0,x=0;char c=getchar();
    while(!isdigit(c))w|=c=='-',c=getchar();
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    return w?-x:x;
}
const int maxn=305,maxm=25005,INF=0x3f3f3f3f;
/*憨憨行为请无视 
struct Edge{
    int to,nxt,h;
}e[maxm];
int ecnt,head[maxn];
inline void addedge(int from,int to,int h)
{
    e[++ecnt]=(Edge){to,head[from],h};head[from]=ecnt;
}
*/
int n,m,t;
int g[maxn][maxn];
int main()
{
    memset(g,INF,sizeof g);
    n=read();m=read();t=read();
    for(int i=1;i<=m;i++)
    g[read()][read()]=read();
    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]));
    for(int a,b,i=1;i<=t;i++)
    {
        a=read(),b=read();
        if(g[a][b]==INF)printf("-1
");
        else printf("%d
",g[a][b]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/BrotherHood/p/13129979.html