Optimal Milking

Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

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头牛,每个挤奶点能最多挤K头牛。下面是矩阵,行和列都表示K个挤奶点,C头牛。矩阵A(i,j)表示i到j的距离。距离都为正值,如果出现0,则表示不直接连通。数据保证有解。问你让这m头牛都能挤奶的条件下,最远的牛最少要走多远。

解题思路:二分枚举距离,每次根据枚举的距离,重新构图。每个挤奶点的匹配次数已知。但是这个题目有一点比较坑,就是二分枚举的时候,r应该从最大值INF开始,因为200只是两点之间的直接距离,floyd之后,可能会出现大于200的距离,应该注意。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
const int INF = 9999999;
const int maxn = 1010;
int Map[maxn][maxn];
int linker[maxn][maxn], used[maxn];
int M;
bool dfs(int u,int rn){
for(int v = 1; v <= rn; v++){
if(used[v] || !Map[u][v]){
continue;
}
used[v] = 1;
if(linker[v][0] < M){
linker[v][++linker[v][0]] = u;
return true;
}else{
for(int j = 1; j <= linker[v][0]; j++){
if(dfs(linker[v][j],rn)){
linker[v][j] = u;
return true;
}
}
}
}
return false;
}
bool Hungary(int ln,int rn){
int ret = 0;
for(int i = 0; i <= rn; i++){
linker[i][0] = 0;
}
for(int i = 1; i <= ln; i++){
memset(used,0,sizeof(used));
if(dfs(i,rn)){
ret++;
}
}
if(ln == ret){
return true;
}
return false;
}
int main(){
int K, C;
int matrix[500][500];
while(scanf("%d%d%d",&K,&C,&M)!=EOF){
int nn = K + C;
for(int i = 1; i <= nn; i++){
for(int j = 1; j <= nn; j++){
scanf("%d",&matrix[i][j]);
if(matrix[i][j] == 0){
matrix[i][j] = INF;
}
}
}
for(int k = 1; k <= nn; k++){
for(int i = 1; i <= nn; i++){
for(int j = 1; j <= nn; j++){
if(matrix[i][j] > matrix[i][k] + matrix[k][j]){
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
}
int l = 1, r = INF, ans;
while(l <= r){ //不会写二分,错了n多次 ORZ
int mid = (l+r)/2;
memset(Map,0,sizeof(Map));
for(int i = K + 1; i <= nn; i++){
for(int j = 1; j <= K; j++){
if(matrix[i][j] <= mid){
Map[i-K][j] = 1;
}
}
}
if(Hungary(C,K)){
r = mid - 1;
ans = mid;
}else{
l = mid + 1;
}
}
printf("%d\n",l);
}
return 0;
}

  


最新文章

  1. 兼容8事件绑定与解绑addEventListener、removeEventListener和ie的attachEvent、detachEvent
  2. Oracl基础知识(一)
  3. Network Assistant (Alpha)版使用说明
  4. jQuery小技巧
  5. Discuz &amp; UCenter 修改手记 - 2014.12.19
  6. Android-- FragmentStatePagerAdapter分页(转载)
  7. 养成好的JAVA编码习惯
  8. WFS
  9. Git使用说明--常用命令
  10. .Net 社区虚拟大会”(dotnetConf)
  11. linux基础知识1
  12. linux的环境变量设置
  13. 使用jQuery的hover事件在IE中不停闪动的解决方法
  14. 转:移植SlidingMenu Android library,和安装example出现的问题解决
  15. Linux驱动技术(八) _并发控制技术
  16. Ubuntu16.04.1安装JDK1.8.0
  17. tomcat中Servlet的工作机制
  18. [Swift]LeetCode892. 三维形体的表面积 | Surface Area of 3D Shapes
  19. springboot整合redis(注解形式)
  20. 超低质量的超低起点PYQT ------前言

热门文章

  1. 一种Web服务的go语言实现
  2. ubuntu - 14.04,安装、配置GO语言开发工具Eclipse!!
  3. GIT版本控制系统(二)
  4. winform 动态添加控件及事件
  5. 【bzoj4939】【YNOI2016】掉进兔子洞(莫队)
  6. DOS下修改IP地址
  7. load xml with xls
  8. socket中close发生的事情,RST,pipe信号错误
  9. ubuntu14.04 apt-get install找不到软件,更换源解决
  10. HDU5952 Counting Cliques计算完全图的个数 巧妙构图+dfs