【POJ】2391Ombrophobic Bovines(二分答案,网络流建图judge,floyd)

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".

题目大意:现在我们在第i个位置上有cows头牛,在第i个位置上有一个屋子,这个屋子可以放置cans[i]头牛,两个屋子之间有的可以到达,但是这样是会花费时间的,下面给出的是n个点,还要m条路径,问你用最少的时间把所有的牛都放到屋子里面

思路:这个题目的话,首先想到的是用网络流或者是二分图的,但是奈何牛的数量有些多,我总不能一个个的拆点吧,所以还是网络流现实一些的,首先要处理每一条路,我们要知道每一条路的长度,也就是他要在某一条路上花费的时间,还有就是我们怎么样去卡这个时间,其实很容易的就想到了二分答案,想到二分答案的基本上就没什么问题了,因为我们可以用这个枚举出来的时间去网络流建边,然后跑最大流看看能不能完成所有的牛都完成的情况,

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
#define INF 1e9
#define INFLL 1LL<<60
#define ll long long
using namespace std;
const int maxn=200*2+10;
struct Edge
{
    int from,to,cap,flow;
    Edge() {}
    Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow) {}
};
struct Dinic
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];

    void init(int n,int s,int t)
    {
        this->n=n,this->s=s,this->t=t;
        for(int i=1; i<=n; i++)
            G[i].clear();
        edges.clear();
    }

    void AddEdge(int from,int to,int cap)
    {
        edges.push_back( Edge(from,to,cap,0) );
        edges.push_back( Edge(to,from,0,0) );
        m = edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS()
    {
        memset(vis,0,sizeof(vis));
        queue<int> Q;
        Q.push(s);
        d[s]=0;
        vis[s]=true;
        while(!Q.empty())
        {
            int x=Q.front();
            Q.pop();
            for(int i=0; i<G[x].size(); i++)
            {
                Edge& e=edges[G[x][i]];
                if(!vis[e.to] && e.cap>e.flow)
                {
                    vis[e.to]=true;
                    d[e.to] = d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x,int a)
    {
        if(x==t || a==0)
            return a;
        int flow=0,f;
        for(int& i=cur[x]; i<G[x].size(); i++)
        {
            Edge& e=edges[G[x][i]];
            if(d[x]+1==d[e.to] && (f=DFS( e.to,min(a,e.cap-e.flow) ) )>0 )
            {
                e.flow +=f;
                edges[G[x][i]^1].flow -=f;
                flow += f;
                a -= f;
                if(a==0)
                    break;
            }
        }
        return flow;
    }

    int Maxflow()
    {
        int flow=0;
        while(BFS())
        {
            memset(cur,0,sizeof(cur));
            flow += DFS(s,INF);
        }
        return flow;
    }
} dinic;
int cows[maxn],cans[maxn];
ll dist[maxn][maxn];
int S,T;
int n,m;
int sum=0;
bool judge(ll mid)
{
    dinic.init(n+n+2,S,T);
    for(int i=1; i<=n; i++)
    {
        dinic.AddEdge(S,i,cows[i]);
        dinic.AddEdge(n+i,T,cans[i]);
    }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
        {
            if(dist[i][j]<=mid)
                dinic.AddEdge(i,n+j,INF);
        }
    if(dinic.Maxflow()==sum)
        return true;
    return false;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        S=0,T=n+n+1;
        sum=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d%d",&cows[i],&cans[i]);
            sum+=cows[i];
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(i==j)
                    dist[i][j]==0;
                else
                    dist[i][j]=INFLL;
        for(int i=1; i<=m; i++)
        {
            int x,y;
            ll dis;
            scanf("%d%d%lld",&x,&y,&dis);
            dist[x][y]=dist[y][x]=min(dist[x][y],dis);
        }
        for(int k=1; k<=n; k++)
            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                    if(dist[i][k]<INFLL && dist[k][j]<INFLL)
                        dist[i][j]=min(dist[i][j], dist[i][k]+dist[k][j]);
        ll l=0,r=0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(dist[i][j]!=INFLL)
                    r=max(r,dist[i][j]);
        //cout<<"l,r:"<<l<<" "<<r<<endl;
        ll mid,ans=-1;
        if(judge(r)==false)
            printf("-1\n");
        else
        {
            while(l<=r)
            {
                mid=(l+r)/2;
                if(judge(mid))
                {
                    ans=mid;
                    r=mid-1;
                }
                else
                    l=mid+1;
            }
            printf("%lld\n",ans);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值