POJ 2391 Ombrophobic Bovines(floyd+二分+最大流+拆点)

87 篇文章 0 订阅
20 篇文章 0 订阅
Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18605 Accepted: 4049

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


题目大意:

    有一些奶牛,在F个牧场,有些牧场有避雨的棚子,每个牧场有一定的奶牛和棚子的容量,牧场之间有一些双向路连接,路的宽度无限,走过需要一定的时间,问最小需要多少时间可以让所有奶牛都到达棚子中


解题思路:

    最小化最大值,明显是二分时间,先用floyd预处理出任意两点的时间,再二分时间上限判断能否到达。

    在建图的时候有一个拆点技巧:我们希望奶牛只走一条预处理之后的边,于是就可以将一个牧场拆成两个点,一个表示初始牧场,另一个表示棚子。这样就可以实现。(具体实现见代码)


AC代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
#define LL long long
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXN=200+3;
const int MAXV=MAXN*2;

struct Edge
{
    LL to,cap,rev;
    Edge(LL t,LL c,LL r):to(t),cap(c),rev(r){}
};

LL dist[MAXN][MAXN],cows[MAXN],save[MAXN],sum;
int F,P;
vector<Edge> G[MAXV];
LL level[MAXV];
LL iter[MAXV];

void add_edge(LL from,LL to,LL cap)
{
    G[from].push_back(Edge(to,cap,G[to].size()));
    G[to].push_back(Edge(from,0,G[from].size()-1));
}

void bfs(LL s)
{
    mem(level,-1);
    queue<LL> que;
    level[s]=0;
    que.push(s);
    while(!que.empty())
    {
        LL v=que.front(); que.pop();
        for(int i=0;i<G[v].size();++i)
        {
            Edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

LL dfs(LL v,LL t,LL f)
{
    if(v==t)
        return f;
    for(LL &i=iter[v];i<G[v].size();++i)
    {
        Edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to])
        {
            LL d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

LL max_flow(LL s,LL t)
{
    int flow=0;
    for(;;)
    {
        bfs(s);
        if(level[t]<0)
            return flow;
        mem(iter,0);
        LL f;
        while((f=dfs(s,t,INF))>0)
            flow+=f;
    }
}

bool judge(LL limit)
{
    for(int i=0;i<=2*F+1;++i)
        G[i].clear();
    //0 ~ F-1    牧场
    //F ~ 2*F-1  雨棚
    //2*F        s
    //2*F+1      t
    LL s=2*F,t=2*F+1;
    for(int i=0;i<F;++i)
    {
        if(cows[i]>0)
            add_edge(s, i, cows[i]);//有奶牛
        if(save[i]>0)
            add_edge(i+F, t, save[i]);//奶牛进入棚子
        add_edge(i, i+F, INF);//一个牧场的奶牛直接进入棚子
        for(int j=0; j<F; ++j)
            if(i!=j&&dist[i][j]<=limit)
                add_edge(i, j+F, INF);//可以在限定时间内进入其它地方的棚子
    }
    LL res=max_flow(s, t);
    return res==sum;
}

int main()
{
    while(~scanf("%d%d",&F,&P))
    {
        mem(dist,0x3f);
        sum=0;
        for(int i=0;i<F;++i)
        {
            scanf("%lld%lld",&cows[i],&save[i]);
            sum+=cows[i];
            dist[i][i]=0;
        }
        for(int i=0;i<P;++i)
        {
            LL u,v,cost;
            scanf("%lld%lld%lld",&u,&v,&cost);
            dist[u-1][v-1]=dist[v-1][u-1]=min(dist[v-1][u-1],cost);
        }
        for(int k=0;k<F;++k)//floyd预处理
            for(int i=0;i<F;++i)
                for(int j=0;j<F;++j)
                    dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
        LL l=-1,r=INF;
        while(r-l>1)
        {
            LL mid=(l+r)/2;
            if(judge(mid))
                r=mid;
            else l=mid;
        }
        printf("%lld\n",r==INF?-1:r);
    }
    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值