poj 2391 Ombrophobic Bovines(floyd+二分+最大流)

Ombrophobic Bovines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8161   Accepted: 1861

Description

FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter. 

The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction. 

Some of the farm's fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse. 

Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.

Input

* Line 1: Two space-separated integers: F and P 

* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field i. 

* Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.

Output

* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".

Sample Input

3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120

Sample Output

110

Hint

OUTPUT DETAILS: 

In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.

Source

USACO 2005 March Gold

题目:http://poj.org/problem?id=2391

分析:这题应该可以想到用二分+最大流判断,先floyd求出个点间的最短路,然后二分边长为答案,用最大流判断是否可行。。。

PS:太久没做题了,这题居然没能一下子想出来,wa,tle,压线过,优化。。。

这题需要优化啊,暴力貌似会tle的= =

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int oo=1000000000;
const int mm=88888;
const int mn=444;
int node,edge,src,dest;
int ver[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn],in[mn],out[mn];
int i,j,k,f,p,sum;
long long d[mn][mn],a[mm];
void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0; i<node; ++i)head[i]=-1;
    edge=0;
}
void addedge(int u,int v,int c)
{
    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
int Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0; i<node; ++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0; l<r; ++l)
        for(i=head[u=q[l]]; i>=0; i=next[i])
            if(flow[i]&&dis[v=ver[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp; i>=0; i=next[i])
        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0; i<node; ++i)work[i]=head[i];
        if(delta=Dinic_dfs(src,oo))ret+=delta;
    }
    return ret;
}
int ok(long long limit)
{
    int i,j;
    prepare(f+f+2,0,f+f+1);
    for(i=1; i<=f; ++i)
    {
        if(in[i])addedge(src,i,in[i]);
        if(out[i])addedge(i+f,dest,out[i]);
        for(j=1; j<=f&&in[i]>0; ++j)
            if(d[i][j]<=limit&&out[j]>0)addedge(i,j+f,oo);
    }
    return(Dinic_flow()==sum);
}
int main()
{
    while(scanf("%d%d",&f,&p)!=-1)
    {
        long long ans=-1;
        int l=0,r=1,m;
        for(sum=0,i=1; i<=f; ++i)scanf("%d%d",&in[i],&out[i]),sum+=in[i];
        for(i=1; i<=f; d[i][i]=0,++i)
            for(j=1; j<=f; ++j)d[i][j]=10000000000000000;
        while(p--)
        {
            scanf("%d%d%d",&i,&j,&k);
            d[i][j]=d[j][i]=min(d[i][j],(long long)k);
        }
        for(k=1; k<=f; ++k)
            for(i=1; i<=f; ++i)
                for(j=1; j<=f; ++j)
                    d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
        for(i=1; i<=f; ++i)
            for(j=i+1; j<=f; ++j)
                if(d[i][j]<10000000000000000)a[r++]=d[i][j];
        sort(a,a+r);
        while(l<=r)
        {
            m=(l+r)>>1;
            if(ok(a[m]))ans=a[m],r=m-1;
            else l=m+1;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(JOIN,优化,Integer,OO,input,output)