POJ2112 Optimal Milking 网络最大流+二分

先用floyd求解任意两点之间的最短距离,再构建网络,将牛与源点相连容量为1,将机器与汇点相连容量为m。然后我们二分枚举答案,将牛与机器之间连线小于枚举值得边加入网络,大于枚举值得边去掉。然后求解最大流
Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 8386 Accepted: 3071
Case Time Limit: 1000MS

Description

FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C.

Each milking point can "process" at most M (1 <= M <= 15) cows each day.

Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.

Input

* Line 1: A single line with three space-separated integers: K, C, and M.

* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.

Output

A single line with a single integer that is the minimum possible total distance for the furthest walking cow.

Sample Input

2 3 2
0 3 2 1 1
3 0 3 2 0
2 3 0 1 0
1 2 1 0 2
1 0 0 2 0

Sample Output

2
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>

using namespace std;

#define MAX 300
#define INF 100000
int k,c,m,n,st,ed;
int edge[MAX][MAX];
int cn[MAX][MAX],dis[MAX],gap[MAX];
void init()
{
	for(int i=1;i<=k+c;i++)
		for(int j=1;j<=k+c;j++)
		{
			scanf("%d",&edge[i][j]);
			if(!edge[i][j])
				edge[i][j]=INF;
		}
}

void build(int maxs)
{
	memset(cn,0,sizeof(cn));
	//初始为0流 
	for(int i=k+1;i<=k+c;i++) cn[0][i]=1; //源点到每头牛设一条容量为1的弧 
	for(int i=1;i<=k;i++) cn[i][k+c+1]=m; //机器到汇点设一条容量为m的弧 
	for(int i=k+1;i<=k+c;i++)
		for(int j=1;j<=k;j++)
			if(edge[i][j]<=maxs) cn[i][j]=1; //牛与机器之间距离小于maxs的加一条容量为1的弧 
}

void floyd()//floyd求出任意两点间最短路 
{
	for(int x=1;x<=k+c;x++)
		for(int i=1;i<=k+c;i++)
			for(int j=1;j<=k+c;j++)
			{
				if(edge[i][j]>edge[i][x]+edge[x][j]) 
					edge[i][j]=edge[i][x]+edge[x][j];
			}
}

int sap(int u,int flow)
{
	if(u==ed)
		return flow;
	int ans=0,i,t;
	for(i=0;i<=n+1;++i)
		if(cn[u][i]&&dis[u]==dis[i]+1)
		{
			t=sap(i,min(flow-ans,cn[u][i]));
			cn[u][i]-=t,cn[i][u]+=t,ans+=t;
			if(ans==flow)
				return ans;
		}
	if(dis[st]>=n+2)
		return ans;
	if(!--gap[dis[u]])  //gap优化 
		dis[st]=n+2;
	++gap[++dis[u]];
	return ans;
}

void solve()
{
	int l,h,m;
	int ans=0;
	n=k+c; 
	st=0,ed=n+1;
	l=0,h=20000;
	floyd();
	while(l<h) //二分枚举答案 
	{
		m=(l+h)/2;
		build(m);
		memset(dis,0,sizeof(dis));
		memset(gap,0,sizeof(gap));
		for(ans=0,gap[0]=n+2;dis[st]<n+2;)    
			ans+=sap(st,INF);
		if(ans==c) 
			h=m;
		else
			l=m+1; 
	} 
	cout<<h<<endl;
} 

int main()
{
	while(cin>>k>>c>>m)
	{
		init();
		solve(); 
	}
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值