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+二分图多重匹配+二分)的更多相关文章

  1. 稳定的奶牛分配 && 二分图多重匹配+二分答案

    题意: 农夫约翰有N(1<=N<=1000)只奶牛,每只奶牛住在B(1<=B<=20)个奶牛棚中的一个.当然,奶牛棚的容量有限.有些奶牛对它现在住的奶牛棚很满意,有些就不太满意 ...

  2. HDU 1669 二分图多重匹配+二分

    Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  3. POJ-2112 Optimal Milking(floyd+最大流+二分)

    题目大意: 有k个挤奶器,在牧场里有c头奶牛,每个挤奶器可以满足m个奶牛,奶牛和挤奶器都可以看成是实体,现在给出两个实体之间的距离,如果没有路径相连,则为0,现在问你在所有方案里面,这c头奶牛需要走的 ...

  4. POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  5. kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树

    二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...

  6. POJ 2112 Optimal Milking (Floyd+二分+最大流)

    [题意]有K台挤奶机,C头奶牛,在奶牛和机器间有一组长度不同的路,每台机器每天最多能为M头奶牛挤奶.现在要寻找一个方案,安排每头奶牛到某台机器挤奶,使得C头奶牛中走过的路径长度的和的最大值最小. 挺好 ...

  7. poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】

    题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K ...

  8. POJ3189:Steady Cow Assignment(二分+二分图多重匹配)

    Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7482   Accepted: ...

  9. POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65 ...

随机推荐

  1. ABAP CDS ON HANA-(8)算術式

    Arithmetic expression in CDS View Allowed Arithmetic operators in CDS view. CDS View- @AbapCatalog.s ...

  2. IDEA中SVN的使用

    文件红色:表示文件没有添加到服务器 绿色:表示没有更新新的修改到服务器 普通黑色:表示和服务器同步 1.如何让修改的文件的父文件也变成蓝色(未提交的状态) 2.其中的1.6 format 1.7 fo ...

  3. 图表制作工具之ECharts

    简介 ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10 ...

  4. babel配置

      首页 首页 首页 博客园 博客园 博客园 联系我 联系我 联系我 demo demo demo GitHub GitHub GitHub 管理 管理 管理 魔魔魔芋芋芋铃铃铃 [02]websto ...

  5. 大数据de 2文章

    点击可免费试用网易有数 文章来源:网易有数的搭积木原则阐述 ,经作者文雯授权发布 wo ceceshi 相关文章:[推荐] SpringBoot入门(五)--自定义配置

  6. Returning Values from Bash Functions

    转自:https://www.linuxjournal.com/content/return-values-bash-functions Bash functions, unlike function ...

  7. static和final的区别(转载)

    Java中static 和final的区别 final定义的变量可以看做一个常量,不能被改变: final定义的方法不能被覆盖: final定义的类不能被继承. final static 就是再加上s ...

  8. python接口自动化: CAS系统验证,自动完成登录并获取token,遇到302请求重定向设置(requests模块 allow_redirects=False)即可

    import requestsimport re import requests import re class Crm_token(object): try: username=int(input( ...

  9. django之上传文件和图片

    文件上传:文件上传功能是网站开发中必定会使用到的技术,在django项目中也是如此,下面会详细讲述django中上传文件的前端和后端的具体处理步骤: 前端HTML代码实现: 1.在前端中,我们需要填入 ...

  10. BZOJ 2756 SCOI2012 奇怪的游戏 最大流

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2756 Description Blinker最近喜欢上一个奇怪的游戏. 这个游戏在一个 N ...