POJ 2391 Ombrophobic Bovines 拆点建图,二分答案,最大流

传送门:点击打开链接

Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15584 Accepted: 3387

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

题意:FJ有F个农场,每个农场有现在用ai头牛,每个农场能容纳bi头牛避雨。农场间有P条双向的路连接,通过每条路都需要花费一定的时间,路的容量无限大。问要让所有的牛都能避雨,至少需要多少时间。

思路:先跑一次Floyd,求出每个农场间的最短路所需的时间。从源点连边到i点,为i农场的牛的数目。从i+n点连边到汇点,容量为i农场能容纳牛来避雨的数目。然后二分答案。如果i和j的最短路小于等于二分的答案。那么建立一条i到j的边,容量为INF.跑一次最大流,检查最大流是不是牛的总数目。

要注意的是,路径的长度总和会超int.需要使用64bit.

ISAP

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL __int64
#define infLL 0x7F7F7F7F7F7F7F7F
#define inf 0x7F7F7F7F
using namespace std;
struct node
{
    int st;
    int en;
    int  flow;
    int next;
} E[510000];
int num;
int p[5100];
void init()
{
    memset(p,-1,sizeof p);
    num=0;
}
void add(int st,int en,int flow)
{
    E[num].st=st;
    E[num].en=en;
    E[num].flow=flow;
    E[num].next=p[st];
    p[st]=num++;
    E[num].st=en;
    E[num].en=st;
    E[num].flow=0;
    E[num].next=p[en];
    p[en]=num++;
}
int T;
int vh[5100];
int h[5100];

int find(int u,int st,int en,int f)
{
    if(u==en)
        return f;
    int left=f;
    int tmp=T;
    for(int i=p[u];i+1;i=E[i].next)
    {
        int v=E[i].en;
        int flow=E[i].flow;
        if(flow)
        {
            if(h[v]+1==h[u])
            {
                int d=min(left,flow);
                d=find(v,st,en,d);
                left-=d;
                E[i].flow-=d;
                E[i^1].flow+=d;
                if(h[st]>=T+1)
                    return f-left;
                if(!left)
                    break;
            }
            if(h[v]<tmp)
                tmp=h[v];
        }
    }
    if(left==f)
    {
        vh[h[u]]--;
        if(vh[h[u]]==0)
            h[st]=T+1;
        h[u]=tmp+1;
        vh[h[u]]++;
    }
    return f-left;
}
int isap(int st,int en,int flow)
{
    memset(vh,0,sizeof vh);
    memset(h,0,sizeof h);
    vh[0]=T+1;
    int ans=0;
    while(h[st]<=T)
        ans+=find(st,st,en,flow);
    return ans;

}
int n,m;
LL g[1005][1005];
int have[1005],hold[1005];
int sum;
bool test(LL t)
{
	T=2*n+5;
	init();
	for(int i=1;i<=n;i++) 
	{
		add(0,i,have[i]);
		add(i+n,2*n+1,hold[i]);
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++) if(g[i][j]<=t) add(i,j+n,inf);
	int res=isap(0,2*n+1,inf);
	if(res==sum) return 1;
	else return 0;
}

int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		sum=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d %d",&have[i],&hold[i]);
			sum+=have[i];
		}
		memset(g,0x7F,sizeof(g));
		for(int i=1;i<=n;i++) g[i][i]=0;
		for(int i=1;i<=m;i++)
		{
			int s,t,l;
			scanf("%d %d %d",&s,&t,&l);
			if(g[s][t]>l)
			{
				g[s][t]=l;
				g[t][s]=l;
			}
		}
		for(int k=1;k<=n;k++)
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		if(g[i][k]!=infLL&&g[k][j]!=infLL&&g[i][j]>g[i][k]+g[k][j]) g[i][j]=g[i][k]+g[k][j];
		LL l=0,r=1000000000000LL;
		while(l<r)
		{
			LL mid=(l+r)>>1;
			if(test(mid)) r=mid;
			else l=mid+1;
		}
		if(l>=1000000000000LL)
		{
			puts("-1");
		}
		else printf("%I64d\n",l);
	}
	return 0;
}

DINIC

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<map>
#include<set>
using namespace std;
#define MP(x,y) make_pair((x),(y))
#define PB(x) push_back(x)
typedef __int64 LL;
//typedef unsigned __int64 ULL;
/* ****************** */
const LL INF=(unsigned __int64)(-1)>>1;
const double INFF=1e200;
const double eps=1e-8;
const LL mod=1000000007;
const LL NN=2005;
const LL MM=1000010;
const LL INFLL=0x7F7F7F7F7F7F7F7F;
/* ****************** */

struct G
{
    LL v,cap,next;
}E[MM];
LL p[NN],T,d[NN],temp_p[NN];
LL qw[NN];

void add(LL u,LL v,LL cap)
{
    E[T].v=v;
    E[T].cap=cap;
    E[T].next=p[u];
    p[u]=T++;

    E[T].v=u;
    E[T].cap=0;
    E[T].next=p[v];
    p[v]=T++;
}

bool find_path(LL st,LL en,LL n)
{
    LL i,u,v,head,tail;
    for(i=0;i<=n;i++)
        d[i]=-1;
    head=tail=0;
    d[st]=0;
    qw[tail]=st;
    while(head<=tail)
    {
        u=qw[head++];
        for(i=p[u];i+1;i=E[i].next)
        {
            v=E[i].v;
            if(d[v]==-1 && E[i].cap>0)
            {
                d[v]=d[u]+1;
                qw[++tail]=v;
            }
        }
    }
    return (d[en]!=-1);
}

LL dfs_flow(LL u,LL& en,LL f)
{
    if(u==en || f==0)
        return f;
    LL flow=0,temp;
    for(; temp_p[u]+1 ; temp_p[u]=E[ temp_p[u] ].next )
    {
        G& e=E[temp_p[u]];
        if(d[u]+1==d[e.v])
        {
            temp=dfs_flow( e.v , en , min(f,e.cap) );
            if(temp>0)
            {
                e.cap-=temp;
                E[ temp_p[u]^1 ].cap+=temp;
                flow+=temp;
                f-=temp;
                if(f==0)
                    break;
            }
        }
    }
    return flow;
}

LL dinic(LL st,LL en,LL n)
{
    LL i,ans=0;
    while( find_path(st,en,n) )
    {
        for(i=0;i<=n;i++)
            temp_p[i]=p[i];

        ans+=dfs_flow(st,en,INF);
    }
    return ans;
}
void init()
{
	memset(p,-1,sizeof(p));
    T=0;
}
LL n,m;
LL g[205][205];
LL have[205],hold[205];
LL sum;
bool test(LL t)
{
	init();
	for(LL i=1;i<=n;i++) 
	{
		add(0,i,have[i]);
		add(i+n,2*n+1,hold[i]);
	}
	for(LL i=1;i<=n;i++)
	for(LL j=1;j<=n;j++) if(g[i][j]<=t) add(i,j+n,INF);
	LL res=dinic(0,2*n+1,2*n+2);
	if(res==sum) return 1;
	else return 0;
}
int main()
{  
    while(scanf("%I64d%I64d",&n,&m)!=EOF)
    {
		sum=0;
		for(LL i=1;i<=n;i++)
		{
			scanf("%I64d %I64d",&have[i],&hold[i]);
			sum+=have[i];
		}
		memset(g,0x7F,sizeof(g));
		for(LL i=1;i<=n;i++) g[i][i]=0;
		for(LL i=1;i<=m;i++)
		{
			LL s,t;
			LL l;
			scanf("%I64d %I64d %I64d",&s,&t,&l);
			if(g[s][t]>l)
			{
				g[s][t]=l;
				g[t][s]=l;
			}
		}
		for(LL k=1;k<=n;k++)
		for(LL i=1;i<=n;i++)
		for(LL j=1;j<=n;j++)
		if(g[i][k]!=INFLL&&g[k][j]!=INFLL&&g[i][j]>g[i][k]+g[k][j]) g[i][j]=g[i][k]+g[k][j];
		LL l=0,r=2000000000000LL;
		while(l<r)
		{
			LL mid=(l+r)>>1;
			if(test(mid)) r=mid;
			else l=mid+1;
		}
		if(l>=2000000000000LL)
		{
			puts("-1");
		}
		else printf("%I64d\n",l);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值