POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)
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 ;
}
POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)的更多相关文章
- 稳定的奶牛分配 && 二分图多重匹配+二分答案
题意: 农夫约翰有N(1<=N<=1000)只奶牛,每只奶牛住在B(1<=B<=20)个奶牛棚中的一个.当然,奶牛棚的容量有限.有些奶牛对它现在住的奶牛棚很满意,有些就不太满意 ...
- HDU 1669 二分图多重匹配+二分
Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/ ...
- POJ-2112 Optimal Milking(floyd+最大流+二分)
题目大意: 有k个挤奶器,在牧场里有c头奶牛,每个挤奶器可以满足m个奶牛,奶牛和挤奶器都可以看成是实体,现在给出两个实体之间的距离,如果没有路径相连,则为0,现在问你在所有方案里面,这c头奶牛需要走的 ...
- POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS Memory Limit: 30000K T ...
- kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树
二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...
- POJ 2112 Optimal Milking (Floyd+二分+最大流)
[题意]有K台挤奶机,C头奶牛,在奶牛和机器间有一组长度不同的路,每台机器每天最多能为M头奶牛挤奶.现在要寻找一个方案,安排每头奶牛到某台机器挤奶,使得C头奶牛中走过的路径长度的和的最大值最小. 挺好 ...
- poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】
题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K ...
- POJ3189:Steady Cow Assignment(二分+二分图多重匹配)
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7482 Accepted: ...
- POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65 ...
随机推荐
- Python3爬虫(六) 解析库的使用之Beautiful Soup
Infi-chu: http://www.cnblogs.com/Infi-chu/ Beautiful Soup 借助网页的结构和属性等特性来解析网页,这样就可以省去复杂的正则表达式的编写. Bea ...
- Git更改远程仓库地址
最近在开发一个公司内部的公共组件库.老大整理了git仓库里的一些项目,其中就包括这个项目. 项目git地址变了,于是我本地的代码提交成功后push失败. 查看远程地址 git remote -v 更改 ...
- Java8新特性(一)——Lambda表达式与函数式接口
一.Java8新特性概述 1.Lambda 表达式 2. 函数式接口 3. 方法引用与构造器引用 4. Stream API 5. 接口中的默认方法与静态方法 6. 新时间日期 API 7. 其他新特 ...
- struts2官方 中文教程 系列八:异常处理
在本教程中,我们将探讨如何启用Struts 2框架处理web应用程序生成的任何未捕获的异常.Struts 2提供了健壮的异常处理,包括能够自动记录任何未捕获的异常,并将用户重定向到错误web页面. 贴 ...
- P1103 书本整理
P1103 书本整理 题目描述 Frank是一个非常喜爱整洁的人.他有一大堆书和一个书架,想要把书放在书架上.书架可以放下所有的书,所以Frank首先将书按高度顺序排列在书架上.但是Frank发现,由 ...
- 可用率map处理
total_data =[ {'event_current_dealer': '陈铁', 'id__count': 66}, {'event_current_dealer': '丁凯', 'id__c ...
- L009文件属性知识详解小节
本堂课分为5部分内容 1.linux下重要目录详解 2.PATH变量路径内容 3.linux系统中文件类型介绍 4.linux系统中文件属性详细介绍 5.linux系统文件属性inode与block知 ...
- 根据生产场景对Linux系统进行分区
转自:http://oldboy.blog.51cto.com/2561410/629558 老鸟谈生产场景如何对linux系统进行分区? █ 前言: 我们买房子时,会考虑1室1厅,2室1厅, ...
- 『JavaScript』模仿接口
JavaScript中并没有内置的创建或实现接口的方法.这里将利用JavaScript的灵活性,来实现与接口意义相同的功能. 什么是接口? 接口的好处: 接口提供了一种用以说明一个对象应该具有哪些方法 ...
- 孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5
孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...