POJ 2391 Ombrophobic Bovines 最大流+二分

Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19257 Accepted: 4170

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个点,p条边,每个点有ai头牛,最多可以容纳bi头牛,每条边可以同时走无数头牛,走过每条边需要ci的时间。要求每个点容纳的牛都不超过最大容量,牛可以随意移动,问最少需要多少时间。

建图:把每个点拆成两个点i和i',源点连i,容量ai ; i'连汇点,容量bi;i点和i'点相连,容量为无穷. 二分所需要的最少时间,将时间超过最少时间的边从图中删去,floyed求原图中每对点的最短时间,再将最短时间不超过当前二分时间的点在流量图中连接,i到j的边连为i到j',容量无穷。
之后跑个最大流,看是不是超过牛的总数,如果是则当前二分的时间符合条件。

开始用vector建邻接表惨遭TLE,链式前向星AC。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int maxn=505,maxk=200005;
const int inf=0x3f3f3f3f;
const ll llinf=1e16;
int current[maxn],dist[maxn],num,c[maxn],b[maxn],head[maxn];
ll a[maxn][maxn];

struct Edge {
	int from,to,flow,pre;
}; 
vector<Edge> edge;

void addedge(int s,int t,int flow) {
	Edge e;
	e.from=s;
	e.to=t;
	e.flow=flow;
	e.pre=head[s];
	edge.push_back(e);
	head[s]=num;
	num++;
	e.from=t;
	e.to=s;
	e.flow=0;
	e.pre=head[t];
	edge.push_back(e);
	head[t]=num;
	num++;
}

int bfs(int n) {
	queue<int> q;
	memset(dist,-1,sizeof(dist));
	dist[0]=0;
	q.push(0);
	while (!q.empty()) {
		int now=q.front();
		q.pop();
		for (int i=head[now];i!=-1;i=edge[i].pre) {
			int k=i,to=edge[i].to;
			if (dist[to]==-1&&edge[k].flow>0) {
				dist[to]=dist[now]+1;
				q.push(to);
			}
		}
	}
	if (dist[n]==-1) return 0; else return dist[n];
}

int dfs(int now,int flow,int t) {
	if (now==t) return flow;
	int f;
	for (int i=current[now];i!=-1;i=edge[i].pre) {
		int k=i,to=edge[i].to;
		current[now]=i;
		if (edge[k].flow>0&&dist[to]==dist[now]+1&&(f=dfs(to,min(flow,edge[k].flow),t))) {
			edge[k].flow-=f;
			edge[k^1].flow+=f;
			return f;
		}
	}
	return 0;
}

int dinic(int t) {
	int f,sum=0;
	while (bfs(t)) {
		for (int i=0;i<=t;i++) current[i]=head[i];
		while (f=dfs(0,inf,t)) sum+=f;
	}
	return sum;
}

void buildgraph(ll mid,int n) {
	int i,j;
	num=0;
	memset(head,-1,sizeof(head));
	for (i=1;i<=n;i++) {
		addedge(0,i,c[i]);
		addedge(i+n,2*n+1,b[i]);
	    addedge(i,i+n,inf);
	    for (j=i+1;j<=n;j++) 
		    if (a[i][j]<=mid) {
		    	addedge(i,j+n,inf);
		    	addedge(j,i+n,inf);
		    }
	}
}

ll solve(int n,ll limit,int sum) {
	ll l=0,r=limit,mid;
	int i;
	ll ans=-1;
	while (l<=r) {
		mid=(l+r)/2;
		buildgraph(mid,n);
		if (dinic(2*n+1)>=sum) {
			ans=mid;
			r=mid-1;
		} else l=mid+1;
		edge.clear();
	}
	return ans;
}

int main() {
	int f,p,i,j,n,k,sum,x,y;
	ll d;
	scanf("%d%d",&n,&p);
	sum=0;
	for (i=1;i<=n;i++) {
		scanf("%d%d",&c[i],&b[i]);
		sum+=c[i];
	}
	ll maxd=0;
	memset(a,0x3f,sizeof(a));
	for (i=1;i<=p;i++) {
		scanf("%d%d%lld",&x,&y,&d);
		if (a[x][y]>d) a[x][y]=d;
		if (a[y][x]>d) a[y][x]=d;
	}
	for (i=1;i<=n;i++) 
		for (j=1;j<=n;j++) 
			for (k=1;k<=n;k++) 
				if (a[j][i]+a[i][k]<a[j][k]) a[j][k]=a[j][i]+a[i][k];
	ll ans=solve(n,llinf,sum);
	printf("%lld\n",ans);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值