poj 3615 Cow Hurdles floyed

floyed 
这个题目能想到就没什么难的了,就是个floyed
    i,j最优的路径i->k->j那么i->k也是最优的。
    所以满足最优子结构,所以就可以像floyed算法那样dp的去解决。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const   int maxn=3e2+9,inf=16843009;
int d[maxn][maxn];
int n,m,t;
int main()
{
//     freopen("in.txt","r",stdin);
    scanf("%d %d%d",&n,&m,&t);
    memset(d,1,sizeof(d));
    for(inti=1,w,from,to;i<=m;i++)
    {
        scanf("%d %d%d",&from,&to,&w);
        d[from][to]=min(d[from][to],w);
    }
    for(intk=1;k<=n;k++)
    for(inti=1;i<=n;i++)
    for(intj=1;j<=n;j++)
//     if(i!=j&&i!=k&&j!=k)
    {
        int tmp=max(d[i][k],d[k][j]);
        d[i][j]=min(d[i][j],tmp);
    }
    for(inti=1,from,to;i<=t;i++)
    {
        scanf("%d%d",&from,&to);
        if(d[from][to]!=inf)
        printf("%d\n",d[from][to]);
        else
        cout<<-1<<endl;
    }
    return 0;
}

你可能感兴趣的:(poj 3615 Cow Hurdles floyed)