POJ 2391 Ombrophobic Bovines(二分建图+网络流+floyd)

                                                                                    Ombrophobic Bovines

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21280 Accepted: 4564

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

题意:有n个棚子,每个棚子有a个奶牛,下雨的时候能容纳b个奶牛。给出棚子之间的路程时间(无向图),问什么时间报天气预报能让所有奶牛都及时躲在棚子里

先跑一个floyd,得出来棚子之间的最短距离,然后二分时间,将每个棚子拆点,源点与左边的点相连,容量为没下雨时的奶牛数量,右边的点与汇点相连,容量为下雨时这个棚子能容纳的奶牛,左边的店与右边的点相连,容量为inf,因为下雨的时候有一些奶牛并不用离开。然后对于所有的路,如果时间小于当前二分的时间,就连一条边。最后跑一遍最大流。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=1e4+7;
const int maxm=1e5+7;
const long long inf=0x3f3f3f3f3f3f3f3f;
typedef long long ll;
struct Node
{
    int to;
    ll capa;
    int next;
}edge[maxm];
int n,m;
int source,sink;
int cnt;
int head[maxn];
int dep[maxn];
ll map[210][210];
ll u[maxn];
ll v[maxn];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    return;
}
void add(int u,int v,ll capa)
{
    edge[cnt].to=v;
    edge[cnt].capa=capa;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].capa=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
    return;
}
bool bfs()
{
    queue<int> que;
    que.push(source);
    memset(dep,-1,sizeof(dep));
    dep[source]=0;
    while(!que.empty())
    {
        int node=que.front();
        que.pop();
        for(int i=head[node];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].capa>0&&dep[v]==-1)
            {
                dep[v]=dep[node]+1;
                if(v==sink) return true;
                que.push(v);
            }
        }
    }
    return dep[sink]!=-1;
}
ll dfs(int node,ll minn)
{
    if(node==sink||minn==0)
    {
        return minn;
    }
    ll r=0;
    for(int i=head[node];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(edge[i].capa>0&&dep[v]==dep[node]+1)
        {
            ll tmp=dfs(v,min(edge[i].capa,minn));
            if(tmp>0)
            {
                edge[i].capa-=tmp;
                edge[i^1].capa+=tmp;
                r+=tmp;
                minn-=tmp;
                if(!minn) break;
            }
        }
    }
    if(!r) dep[node]=-1;
    return r;
}
ll dinic()
{
    ll maxflow=0;
    while(bfs())
    {
        maxflow+=dfs(source,inf);
    }
    return maxflow;
}
void floyd()
{
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
            }
        }
    }
    return;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(~scanf("%d%d",&n,&m))
    {
        source=0;
        sink=n*2+1;
        ll cow_sum=0;
        for(int i=1;i<=n;i++)
        {
            ll b;
            ll a;
            scanf("%lld%lld",&a,&b);
            u[i]=a;
            v[i]=b;
            cow_sum+=a;
        }
        memset(map,inf,sizeof(map));
        for(int i=0;i<m;i++)
        {
            int u,v;
            ll w;
            scanf("%d%d%lld",&u,&v,&w);
            map[u][v]=map[v][u]=min(map[u][v],w);
        }
        floyd();
        ll left=0;
        ll right=inf;
        ll ans=-1ll;
        while(left<=right)
        {
            ll mid=(left+right)>>1;
            init();
            for(int i=1;i<=n;i++)
            {
                add(source,i,u[i]);
                add(i+n,sink,v[i]);
                add(i,i+n,inf);
                for(int j=1;j<=n;j++)
                {
                    if(i==j) continue;
                    else
                    {
                        if(map[i][j]<=mid)
                        {
                            add(i,j+n,inf);
                        }
                    }
                }
            }
            if(dinic()>=cow_sum)
            {
                ans=mid;
                right=mid-1;
            }
            else
            {
                left=mid+1;
            }
        }
        printf("%lld\n",ans==inf?-1:ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值