【BZOJ1738】【POJ2391】【Usaco2005 mar】 Ombrophobic Bovines 发抖的牛 网络流

题意:

约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.    牛们在农场的F(1≤F≤200)个田地上吃草.有P(1≤P≤1500)条双向路连接着这些田地.路很宽,无限量的牛可以通过.田地上有雨棚,雨棚有一定的容量,牛们可以瞬间从这块田地进入这块田地上的雨棚    请计算最少的时间,让每只牛都进入雨棚.

input

第1行:两个整数F和P;

第2到F+1行:第i+l行有两个整数描述第i个田地,第一个表示田地上的牛数,第二个表示田地上的雨棚容量.两个整数都在0和1000之间.

第F+2到F+P+I行:每行三个整数描述一条路,分别是起点终点,及通过这条路所需的时间(在1和10^9之间).

output

一个整数,表示最少的时间.如果无法使牛们全部进入雨棚,输出-1.

题解:二分+最大流check。

代码:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 405
#define G 100000
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3fll
#define LL long long
using namespace std;
struct KSD
{
	int v,next,len;
}e[G];
int head[N],cnt;
void add(int u,int v,int len)
{
	cnt++;
	e[cnt].v=v;
	e[cnt].len=len;
	e[cnt].next=head[u];
	head[u]=cnt;
}
queue<int>q;
int d[N],s,t;
bool bfs()
{
	memset(d,0,sizeof(d));
	int i,u,v;
	while(!q.empty())q.pop();
	q.push(s),d[s]=1;
	while(!q.empty())
	{
		u=q.front(),q.pop();
		for(i=head[u];i;i=e[i].next)if(e[i].len)
		{
			v=e[i].v;
			if(!d[v])
			{
				d[v]=d[u]+1;
				if(v==t)
					return 1;
				q.push(v);
			}
		}
	}
	return 0;
}
int dinic(int x,int flow)
{
	if(x==t)return flow;
	int remain=flow,k;
	int i,v;
	for(i=head[x];i&&remain;i=e[i].next)
	{
		v=e[i].v;
		if(e[i].len&&d[v]==d[x]+1)
		{
			k=dinic(v,min(remain,e[i].len));
			if(!k)d[v]=0;
			e[i^1].len+=k,e[i].len-=k;
			remain-=k;
		}
	}
	return flow-remain;
}
int n,m;
int cow[N],contain[N],sumcow;
LL map[N][N];
void build(LL mid)
{
	int i,j,k;
	s=n*2+1,t=n*2+2;
	cnt=1;
	memset(head,0,sizeof(head));
	for(i=1;i<=n;i++)
	{
		add(s,i*2-1,cow[i]),add(i*2-1,s,0);
		add(i*2,t,contain[i]),add(t,i*2,0);
	}
	for(i=1;i<=n;i++)for(j=1;j<=n;j++)if(map[i][j]<=mid)add(i*2-1,j*2,inf),add(j*2,i*2-1,0);
}
bool check(LL mid)
{
	build(mid);
	int maxflow=0;
	while(bfs())maxflow+=dinic(s,inf);
	if(maxflow==sumcow)return 1;
	else return 0;
}
bool work()
{
	int i,j,k;
	int a,b;LL c;
	if(scanf("%d%d",&n,&m)==EOF)return 0;
	for(sumcow=0,i=1;i<=n;i++)scanf("%d%d",&cow[i],&contain[i]),sumcow+=cow[i];
	memset(map,0x3f,sizeof(map));
	for(i=1;i<=n;i++)map[i][i]=0;
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%lld",&a,&b,&c);
		if(map[a][b]>c)map[a][b]=map[b][a]=c;
	}
	for(k=1;k<=n;k++)for(i=1;i<=n;i++)for(j=1;j<=n;j++)map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
	LL l=0,r=INF-1,mid,ans;
	while(l<r)
	{
		if(r-l<=3)
		{
			ans=-1;
			for(LL I=r;I>=l;I--)if(check(I))ans=I;
			break;
		}
		mid=l+r>>1;
		if(check(mid))r=mid;
		else l=mid+1;
	}
	printf("%lld\n",ans);
	return 1;
}
int main()
{
//	freopen("test.in","r",stdin);
	while(work());
}
时间: 2024-11-05 11:53:20

【BZOJ1738】【POJ2391】【Usaco2005 mar】 Ombrophobic Bovines 发抖的牛 网络流的相关文章

BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 shelter 点, 然后对于每个 farm x : S -> cow( x ) = cow( x ) 数量 , shelter( x ) -> T = shelter( x ) 容量 ; 对于每个dist( u , v ) <= m 的 cow( u ) -> shelter( v

BZOJ1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛

先预处理出来每个点对之间的最短距离 然后二分答案,网络流判断是否可行就好了恩 1 /************************************************************** 2 Problem: 1738 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:404 ms 7 Memory:9788 kb 8 **********************************************

BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛

Description 约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.    牛们在农场的F(1≤F≤200)个田地上吃草.有P(1≤P≤1500)条双向路连接着这些田地.路很宽,无限量的牛可以通过.田地上有雨棚,雨棚有一定的容量,牛们可以瞬间从这块田地进入这块田地上的雨棚    请计算最少的时间,让每只牛都进入雨棚. Input 第1行:两个整数F和P; 第2到F+1行:第i+l行有两个整数描述第i个田

POJ 2391 Ombrophobic Bovines (二分,最短路径,网络流sap,dinic,预留推进 )

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14019   Accepted: 3068 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 h

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

POJ 2391 Ombrophobic Bovines 链接:http://poj.org/problem?id=2391 题目:农场有F 块草地,1≤F≤200,奶牛们在草地上吃草.这些草地之间有P 条路相连,1≤P≤1500,这些路足够宽,再多的奶牛也能同时在路上行走.有些草地上有避雨点,奶牛们可以在此避雨.避雨点的容量是有限的,所以一个避雨点不可能容纳下所有的奶牛.草地与路相比很小,奶牛们通过时不需要花费时间.计算警报至少需要提前多少时间拉响,以保证所有的奶牛都能到达一个避雨点. 思路:

解题报告 之 POJ2391 Ombrophobic Bovines

解题报告 之 POJ2391 Ombrophobic Bovines 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 rai

POJ2391 Ombrophobic Bovines(网络流)(拆点)

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18205   Accepted: 3960 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 h

poj2391 Ombrophobic Bovines

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20459   Accepted: 4403 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 h

POJ2391:Ombrophobic Bovines

Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 21660Accepted: 4658 题目链接:http://poj.org/problem?id=2391 Description: FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes the