Optimal Milking

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 20262   Accepted: 7230
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

题意:

C只奶牛,K个奶牛机,现在每只奶牛都要走到一个奶牛机,但是每个奶牛机只能容纳一定数量的奶牛,问奶牛到达奶牛机最远路径的最小值是多少。

题解:

这题可以建模为二分图多重匹配,并且根据题目要求,我们可以想到二分最远距离。但是此题需要注意的是,题目中给出的距离是直接的点与点之间的距离,但是奶牛走到奶牛机并不一定只有一条路径。

所以我们可以通过Floyd预处理一下(有点贪心的思想),求出两点间的最短距离。

如果没有通过Floyd预处理,那么最后二分出来的值会有偏差。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mem(x) memset(x,0,sizeof(x))
#define INF 10000000
using namespace std; const int N = ;
int k,c,m;
int mid,ylink[N][N],vy[N],check[N],d[N][N]; inline int dfs(int x){
for(int i=;i<=k;i++){
if(!check[i]){
if(d[x][i]<=mid) check[i]=;
else continue ;
if(vy[i]<m){
ylink[i][++vy[i]]=x;
return ;
}
for(int j=;j<=vy[i];j++){
int now = ylink[i][j];
if(dfs(now)){
ylink[i][j]=x;
return ;
}
}
}
}
return ;
} inline int Check(int x){
mem(vy);mem(ylink);
for(int i=k+;i<=k+c;i++){
mem(check);
if(!dfs(i)) return ;
}
return ;
} int main(){
scanf("%d%d%d",&k,&c,&m);
for(int i=;i<=k+c;i++) for(int j=;j<=k+c;j++) scanf("%d",&d[i][j]);
for(int i=;i<=k+c;i++)
for(int j=;j<=k+c;j++)
if(i!=j &&!d[i][j]) d[i][j]=INF;
for(int t=;t<=k+c;t++)for(int i=;i<=k+c;i++)for(int j=;j<=k+c;j++)
if(d[i][j]>d[i][t]+d[t][j]) d[i][j]=d[i][t]+d[t][j];
int l=,r=INF+,Ans;
while(l<=r){
mid=l+r>>;
if(Check(mid)){
r=mid-;
Ans=mid;
}else l=mid+;
}
printf("%d\n",Ans);
return ;
}

最新文章

  1. Mono+Jexus让C#运行在Linux(centos7_x64),学习笔记
  2. Vuejs使用笔记 --- component内部实现
  3. 每天一命令 git reset
  4. Redisd VS Memcached
  5. tornado和django的结合使用 tornado Server for django WSGI APP
  6. 数据库Error:The ScriptCollection in ScriptName not find
  7. cocos2d 遍历CCAarray
  8. Miller-Rabin素数测试学习小计
  9. 属性观察者willSet与didSet
  10. css盒子模型、文档流、相对与绝对定位、浮动与清除模型
  11. django是怎么处理请求的
  12. 201521123033《Java程序设计》第10周学习总结
  13. Js中for循环的阻塞机制
  14. [SCOI2012]滑雪与时间胶囊
  15. 自己总结的C#编码规范--7.文档下载 &amp; 总结
  16. CentOS 7 部署Gitlab+Jenkins持续集成(CI)环境
  17. jQuery 让input里面的内容可以布局到页面上
  18. java笔试之输出
  19. back
  20. gulp 编译es6 react 教程 案例 配置

热门文章

  1. 05 redis(进阶)
  2. Django之频率组件
  3. ABAP CDS ON HANA-(1)CDSビュー作成
  4. python2.7练习小例子(二十八)
  5. Xshell 清除历史记录方法
  6. 【数据库】 SQLite 介绍
  7. Log4net的一个小例子
  8. eclipse 关闭validating
  9. Sublime text3最全快捷键清单
  10. CCF-NOIP-2018 提高组(复赛) 模拟试题(三)