Optimal Milking——二分图多重最大匹配+二分+floyd求最短路

题目链接:http://poj.org/problem?id=2112

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

题目翻译

‎FJ 已经把他的 K (1 <= K <= 30) 挤奶机移入 C(1 <= C <= 200) 母牛的奶牛牧场。奶牛和挤奶机之间有一组不同长度的路径。挤奶机位置按 ID 号 1.K 命名;奶牛位置由 ID 号 K+1.K+C‎

‎命名。每个挤奶点每天最多只能"处理"M (1 <= M <= 15) 母牛。‎
‎编写一个程序,找到每头奶牛到一些挤奶机的分配,以便尽可能缩短最远的奶牛旅行距离(当然,挤奶机不会被‎
‎过度使用)。所有输入数据集至少可以进行一个合法分配。奶牛在去挤奶机的路上可以穿过几条小路。‎

‎输入‎

‎• 第 1 行:具有三个空格分隔整数的单行:K、C 和‎
‎M。‎
‎* 行 2....: K&C 空间分隔整数的每一条 K&C 线都描述了各种实体对之间的距离。输入形成对称矩阵。第 2 行告诉从挤奶机 1 到每个其他实体的距离;第 3 行指示从机器 2 到每个其他实体的距离,等等。由路径直接连接的实体的距离是正整数,不大于 200。未由路径直接连接的实体之间的距离为 0。实体到自身的距离(即对角线上的所有数字)也给出为 0。为了保持合理长度的输入行,当 K+C > 15 时,将一行分成 15 个数字的连续行和一条可能较短的行来完成一行。每个新行从自己的行开始。‎

‎输出‎

‎单个整数的单行,这是最远行走的母牛的最小总距离。‎

 

经典二分图多重最大匹配问题,我们需要先用Floyd求出各个点的最短路距离,先求出最大距离,然后二分枚举距离,代入二分图多重最大匹配判断是否符合条件。求出符合条件的最小距离。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 257;
const int INF = 0x3f3f3f3f;
int G[N][N],match[N][N],cnt[N],map[N][N];
int visited[N];
int k,c,m;
void floyd(){
	for(int t = 1;t <= k+c;++t)
		for(int i = 1;i<=k+c;++i)
			for(int j = 1;j<=k+c;++j)
				G[i][j]=min(G[i][j],G[i][t]+G[t][j]);
}
int dfs(int x){
	for(int i = 1;i<=k;++i){
		if(visited[i]||!map[i][x]) continue;
		visited[i]=1;
		if(cnt[i]<m){
			match[i][cnt[i]++]=x;
			return 1;
		}
		else{
			for(int j =0;j<cnt[i];++j){
				if(dfs(match[i][j])){
					match[i][j]=x;
					return 1;
				}
			}
		}
	}
	return 0;
}
bool judge(int n){
	memset(cnt,0,sizeof(cnt));
	memset(map,0,sizeof(map));
	memset(match,0,sizeof(match));
	for(int i = 1;i<=k;++i){
		for(int j = k+1;j<=k+c;++j){
			if(G[i][j]<=n)
			   map[i][j-k]=1;
		}
	}
	for(int i = 1;i<=c;++i){
		memset(visited,0,sizeof(visited));
		if(!dfs(i)) return false;
	}
	return true;
}
int main(int argc, char** argv) {
	while(cin>>k>>c>>m){
		for(int i = 1;i<=k+c;++i){
			for(int j = 1;j<=k+c;++j){
				cin>>G[i][j];
				if(!G[i][j]) G[i][j]=INF;
			}
		}
		floyd();
		int r=0;
		for(int i = 1;i<=k;++i)
			for(int j = k+1;j<=k+c;++j)
				r=max(r,G[i][j]);
		int l=0,mid;
		while(l<r){
			mid=(r-l)/2+l;
			if(judge(mid)) r=mid;
			else l=mid+1;
		}
		cout<<r<<endl;
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值