POJ2112

6 篇文章 0 订阅
5 篇文章 0 订阅


Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 13302 Accepted: 4818
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


题目大意为给你一个无向图,其中有K头机器C头牛,每头机器可以给M头牛喂奶,问所有牛都可以喝奶时,走得最远的牛的最短路程。

做法为最短路 + 二分 + 最大流:
Floyd求出点之间的最短路
再二分走得最远的牛的路程Limit
将S和Machines连边,容量为M;将Cows和T连边,容量为1;再将Machines中到Cows距离小于Limit的点对连边,容量为1;求最大流是否等于C,若等于说明走得最远的牛的路程小于Limit(每次确定Limit都要重新建立网络)。

代码如下:
/* 
* @Author: duyixian
* @Date:   2015-04-15 19:07:32
* @Last Modified by:   duyixian
* @Last Modified time: 2015-04-19 22:03:21
*/

#include "cstdio"
#include "cstdlib"
#include "iostream"
#include "algorithm"
#include "queue"
#include "cstring"

using namespace std;

#define MAX_SIZE 300
#define INF 0x3F3F3F3F
struct Edge
{
	int To, Next, C;
}Edges[50000];

int K, C, M, S, T, Total = 1;
int Front[MAX_SIZE], Distance[MAX_SIZE][MAX_SIZE], Depth[MAX_SIZE];

inline void Add_Edge(int From, int To, int C)
{
	++Total;
	Edges[Total].To = To;
	Edges[Total].C = C;
	Edges[Total].Next = Front[From];
	Front[From] = Total;
}

inline void Add_Edges(int From, int To, int C)
{
	Add_Edge(From, To, C);
	Add_Edge(To, From, 0);
}

inline void Build(int Limit)
{
	Total = 1;
	memset(Front, 0, sizeof(Front));
	for (int i = 1; i <= K; ++i)
	{
		Add_Edges(S, i, M);
	}
	for (int i = K + 1; i <= K + C; ++i)
	{
		Add_Edges(i, T, 1);
	}
	for (int i = 1; i <= K; ++i)
		for (int j = K + 1; j <= K + C; ++j)
			if(Distance[i][j] <= Limit)
				Add_Edges(i, j, 1);
}

void Floyd()
{
	for(int k = 1; k <= K + C; ++k)
		for(int i = 1; i <= K + C; ++i)
			for(int j = 1; j <= K + C; ++j)
				if(Distance[i][j] > Distance[i][k] + Distance[k][j])
					Distance[i][j] = Distance[i][k] + Distance[k][j];
}

bool BFS()
{
	memset(Depth, 0, sizeof(Depth));
	Depth[S] = 1;
	queue<int> Queue;
	Queue.push(S);
	while(!Queue.empty())
	{
		int Now = Queue.front();
		Queue.pop();
		for(int temp = Front[Now]; temp; temp = Edges[temp].Next)
			if(Edges[temp].C && !Depth[Edges[temp].To])
			{
				Depth[Edges[temp].To] = Depth[Now] + 1;
				Queue.push(Edges[temp].To);
			}
	}
	return Depth[T];
}

int DFS(int Now, int In)
{
	int Rest = In;
	if(Now == T)
		return In;
	for(int temp = Front[Now]; temp; temp = Edges[temp].Next)
	{
		if(Edges[temp].C && Depth[Edges[temp].To] == Depth[Now] + 1)
		{
			int Increment = DFS(Edges[temp].To, min(Rest, Edges[temp].C));
			Rest -= Increment;
			Edges[temp].C -= Increment;
			Edges[temp ^ 1].C += Increment;
			if(!Rest)
				return In;
		}
	}
	if(Rest == In)
		Depth[Now] = 0;
	return In - Rest;
}

int Max_Flow(int Limit)
{
	int Ans = 0;
	Build(Limit);
	while(BFS())
	{
		Ans += DFS(S, INF);
	}
	return Ans;
}

int main()
{
	cin >> K >> C >> M;
	S = 0;
	T = K + C + 1;
	for(int i = 1; i <= K + C; ++i)
		for(int j = 1; j <= K + C; ++j)
		{
			scanf("%d", &Distance[i][j]);
			if(!Distance[i][j])
				Distance[i][j] = INF;
		}
	Floyd();
	int Left = 0, Right = 100000;
	int Mid = (Left + Right) / 2;
	while(Mid != Left && Mid != Right)
	{
		int Flow;
		Flow = Max_Flow(Mid);
		if(Flow == C)
		{
			Right = Mid;
			Mid = (Left + Right) / 2;
		}
		else
		{
			Left = Mid;
			Mid = (Left + Right) / 2;
		}
	}
	cout << Right;
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值