poj 2391 Ombrophobic Bovines 二分+最大流

同poj 2112.

代码:

//poj 2391
//sep9
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long ll;

const int maxN=1024;
const int maxM=100002;
const ll MAX=(1ULL<<63)-1; 

struct Edge
{
	int v,f,nxt;
}e[maxM*2+10];

queue<int> que;
int src,sink;
int g[maxN+10];
int nume;
bool vis[maxN+10];
int dist[maxN+10];

ll d[maxN][maxN];
int in[maxN],out[maxN];
int f,p,sum;

void addedge(int u,int v,int c)
{
	e[++nume].v=v;e[nume].f=c;e[nume].nxt=g[u];g[u]=nume;
	e[++nume].v=u;e[nume].f=0;e[nume].nxt=g[v];g[v]=nume;
}

void init()
{
	memset(g,0,sizeof(g));
	nume=1;
}

int bfs()
{
	while(!que.empty()) que.pop();
	memset(dist,0,sizeof(dist));
	memset(vis,0,sizeof(vis));
	vis[src]=true;
	que.push(src);
	while(!que.empty()){
		int u=que.front();que.pop();
		for(int i=g[u];i;i=e[i].nxt)
			if(e[i].f&&!vis[e[i].v]){
				que.push(e[i].v);
				dist[e[i].v]=dist[u]+1;
				vis[e[i].v]=true;
				if(e[i].v==sink)
					return 1;
			}
	}
	return 0;
}

int dfs(int u,int delta)
{
	if(u==sink)
		return delta;
	int ret=0;
	for(int i=g[u];ret<delta&&i;i=e[i].nxt)
		if(e[i].f&&dist[e[i].v]==dist[u]+1){
			int dd=dfs(e[i].v,min(e[i].f,delta-ret));
			if(dd>0){
				e[i].f-=dd;
				e[i^1].f+=dd;
				ret+=dd;
			}
			else
				dist[e[i].v]=-1;
		}
	return ret;
}

int dinic()
{
	int ret=0;
	while(bfs()==1)
		ret+=dfs(src,INT_MAX);
	return ret;
}
bool work(ll max_len)
{
	init();
	src=0,sink=2*f+1;
	for(int i=1;i<=f;++i)
		addedge(src,i,in[i]);
	for(int i=1;i<=f;++i)
		for(int j=1;j<=f;++j)
			if(d[i][j]<=max_len)
				addedge(i,j+f,INT_MAX);
	for(int j=f+1;j<=2*f;++j)
		addedge(j,sink,out[j-f]);
	if(dinic()==sum)
		return true;
	return false;
}

int main()
{
	scanf("%d%d",&f,&p);
	for(int i=1;i<=f;++i){
		for(int j=1;j<=f;++j)
			d[i][j]=d[j][i]=MAX;
		d[i][i]=0;
	}
	sum=0;
	for(int i=1;i<=f;++i){
		scanf("%d%d",&in[i],&out[i]);
		sum+=in[i];
	}
	while(p--){
		int a,b;
		ll c;
		scanf("%d%d%I64d",&a,&b,&c);
		c=min(d[a][b],c);
		d[a][b]=d[b][a]=c;
	}
	for(int k=1;k<=f;++k)
		for(int i=1;i<=f;++i)
			for(int j=1;j<=f;++j)
				if(d[i][k]!=MAX&&d[k][j]!=MAX)
					d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
	ll l=0,r=MAX/2,mid,ans=-1;
	while(l<r){
		mid=(l+r)/2;
		if(work(mid)){
			ans=mid;
			r=mid;
		}else
			l=mid+1;
	}
	printf("%I64d",ans);
	return 0;
}
 
时间: 2024-10-13 00:52:36

poj 2391 Ombrophobic Bovines 二分+最大流的相关文章

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

POJ 2391 Ombrophobic Bovines 题目链接 题意:一些牛棚,有a只牛,现在下雨,每个牛棚容量量变成b,现在有一些道路连接了牛棚,问下雨后牛走到其他牛棚,使得所有牛都有地方躲雨,最后一只牛要走多久 思路:二分答案,然后最大流去判断,建图的方式为,牛棚拆点,源点连向入点,容量为a,出点连向汇点容量为b,中间入点和出点之间根据二分的值判断哪些边是可以加入的 代码: #include <cstdio> #include <cstring> #include <

POJ 2391 —— Ombrophobic Bovines 二分+Floyd+最大流

原题:http://poj.org/problem?id=2391 #include<cstdio> #include<cstring> #include<string> #include<queue> #include<vector> #define inf 1LL<<60 #define ll long long using namespace std; const int maxn = 500; const int maxm =

POJ 2391 Ombrophobic Bovines 不喜欢雨的奶牛 Floyd+二分枚举+最大流

题目链接:POJ 2391 Ombrophobic Bovines Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15006   Accepted: 3278 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes

poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic

poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 15时39分22秒 * File Name: poj2391.cpp */ #include <ctime> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring&g

poj 2391 Ombrophobic Bovines(最大流+floyd+二分)

Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 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 de

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

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

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 网络流 拆点

Poj 2391 Ombrophobic Bovines 网络流 拆点 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 approa

POJ 2391 Ombrophobic Bovines

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