Description

FJ has moved his K ( <= K <= ) milking machines out into the cow pastures among the C ( <= C <= ) 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 ..K; the cow locations are named by ID numbers K+..K+C. 

Each milking point can "process" at most M ( <= M <= ) 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 : A single line with three space-separated integers: K, C, and M. 

* Lines .. ...: 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  tells the distances from milking machine  to each of the other entities; line  tells the distances from machine  to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than . Entities not directly connected by a path have a distance of . The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as . To keep the input lines of reasonable length, when K+C > , a row is broken into successive lines of  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


Sample Output


Source

 
 

题意:K个产奶机,C头奶牛,每个产奶机最多可供M头奶牛使用;并告诉了产奶机、奶牛之间的两两距离Dij(0<=i,j<K+C)。

问题:如何安排使得在任何一头奶牛都有自己产奶机的条件下,奶牛到产奶机的最远距离最短?最短是多少?

1、首先floyd求出最短距离
2、二分答案, 重新建图,把多重匹配的点分裂成多个点来解二分图的最大匹配
3、看看二分的答案是否符合全部牛的匹配情况,然后继续二分
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 206
#define inf 1<<29
int k,c,m;
int mp[][];
int path[N][];
int match[];
int vis[];
void flyod(){
for(int L=;L<=k+c;L++){
for(int i=;i<=k+c;i++){
for(int j=;j<=k+c;j++){
if(mp[i][j]>mp[i][L]+mp[L][j]){
mp[i][j]=mp[i][L]+mp[L][j];
}
}
}
}
}
void changePath(int mid){
for(int i=;i<=c;i++){
for(int j=;j<=k;j++){
if(mp[k+i][j]<=mid){
for(int t=;t<=m;t++){
path[i][(j-)*m+t]=;
}
}
}
}
}
bool dfs(int x){
for(int i=;i<=k;i++){
for(int j=;j<=m;j++){
int u=(i-)*m+j;
if(path[x][u] && !vis[u]){
vis[u]=;
if(match[u]==- || dfs(match[u])){
match[u]=x;
return true;
}
}
}
}
return false;
}
bool judge(){ memset(match,-,sizeof(match));
for(int i=;i<=c;i++){
memset(vis,,sizeof(vis));
if(!dfs(i)){
return false;
}
}
return true; }
void solve(){
int L=,R=;
while(L<R){
int mid=(L+R)>>;
memset(path,,sizeof(path));
changePath(mid);
if(judge()){
R=mid;
}else{
L=mid+;
}
}
printf("%d\n",L);
}
int main()
{
while(scanf("%d%d%d",&k,&c,&m)==){ for(int i=;i<=k+c;i++){
for(int j=;j<=k+c;j++){
scanf("%d",&mp[i][j]);
if(mp[i][j]==){
mp[i][j]=inf;
}
}
} flyod();
solve();
}
return ;
}

 附上有注释的代码:

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> const int MAXK = + ;
const int MAXC = + ;
const int MAXM = + ;
const int INF = ; using namespace std; int k, c, m;
int map[MAXK+MAXC][MAXK+MAXC];
bool path[MAXC][MAXK*MAXM];
int match[MAXK*MAXM];
bool vst[MAXK*MAXM]; /* 把每个挤奶器点分裂成 m 个点,选边权 <=tmp 的边建立二分图 */
void buildGraph(int tmp)
{
memset(path, false, sizeof(path)); for (int i=; i<=c; i++)
for (int j=; j<=k; j++)
if (map[k+i][j] <= tmp)
{
for (int t=; t<=m; t++)
{
path[i][(j-)*m+t] = true;
}
}
} bool DFS(int i)
{
for (int j=; j<=k*m; j++)
{
if (path[i][j] && !vst[j])
{
vst[j] = true;
if (match[j] == - || DFS(match[j]))
{
match[j] = i;
return true;
}
}
}
return false;
} /* 针对该题,做了小小的修改,全部匹配返回 true, 否则返回 false */
bool maxMatch()
{
memset(match, -, sizeof(match));
for (int i=; i<=c; i++)
{
memset(vst, false, sizeof(vst));
if (!DFS(i))
return false;
}
return true;
} /* 二分答案,求二分图最大匹配 */
void solve()
{
int low = , high = *(k+c), mid;
while (low < high)
{
mid = (low + high)/;
buildGraph(mid);
maxMatch() == true ? high = mid : low = mid+;
}
printf("%d\n", low);
} void floyd()
{
int i, j, h, t = k+c;
for (h=; h<=t; h++)
for (i=; i<=t; i++)
for (j=; j<=t; j++)
if (map[i][j] > map[i][h]+map[h][j])
map[i][j] = map[i][h]+map[h][j];
} 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", &map[i][j]);
if (map[i][j] == )
map[i][j] = INF;
}
floyd();
solve();
return ;
}

poj 2112 Optimal Milking (二分图匹配的多重匹配)的更多相关文章

  1. Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)

    题目链接: Poj 2112 Optimal Milking 题目描述: 有k个挤奶机,c头牛,每台挤奶机每天最多可以给m头奶牛挤奶.挤奶机编号从1到k,奶牛编号从k+1到k+c,给出(k+c)*(k ...

  2. POJ 2112 Optimal Milking (二分+最短路径+网络流)

    POJ  2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K To ...

  3. POJ 2112 Optimal Milking (二分 + floyd + 网络流)

    POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...

  4. POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】

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

  5. POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)

    题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远   输入数据: 第一行三个数 K, C, M  接下来是   ...

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

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

  7. POJ 2112: Optimal Milking【二分,网络流】

    题目大意:K台挤奶机,C个奶牛,每台挤奶器可以供M头牛使用,给出奶牛和和机器间的距离矩阵,求所有奶牛走最大距离的最小值 思路:最大距离的最小值,明显提示二分,将最小距离二分之后问题转化成为:K台挤奶机 ...

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

    题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...

  9. POJ 2112 Optimal Milking (Dinic + Floyd + 二分)

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 19456   Accepted: 6947 ...

随机推荐

  1. VS2010旗舰版安装图解

    微软公布了最新的 Visual Studio 2010 软件开发编程平台及 .Net Framework 4 框架.这次 VisualStudio 2010 包含 Professional 专业版.P ...

  2. Redis Install

    Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部分场合可以对关系数据库起到很好的补充作用.它提供了Pytho ...

  3. [iOS] App引导页的简单实现 (Swift 2)

    转载请注明出处:http://www.jianshu.com/p/024dd2d6e6e6# 已更新至 Xcode7.2.Swift2.1 在第一次打开App或者App更新后通常用引导页来展示产品特性 ...

  4. ubuntu14.04 install flow.

    打开虚拟机,点击菜单上的“文件”,选择新建虚拟机,如下图所示: 注释:这里选择自定义安装,点击下一步. 这里我的虚拟机版本最新是10的,就选最新的,然后点击下一步,如下图: 这里选择要安装的Ubunt ...

  5. Eclipse 添加快捷方式

    1.在/usr/share/applications创建一个desktop文件,命名为eclipse.desktop 文件内容如下 [Desktop Entry]Name=EclipseType=Ap ...

  6. Linux 创建swap分区

    --首先分出一个分区 /dev/sda5 (注意分区类型)mkswap /dev/sda5           --格式化分区成swap格式swapon /dev/sda5           --激 ...

  7. 对html制作新手的一些建议,大牛可以忽略

    本篇主要讲前端并给制作html页面的新手一些建议,大牛勿喷大牛可以绕过. 感受:我是搞后端开发的,有时拿到一些静态(Html)页面,看到里面的页面结构命名规则极不规范,就有点不好的 感觉了.当然出现这 ...

  8. 跨域访问-JSONP

    JSONP即JSON with Padding.由于同源策略的限制,XmlHttpRequest只允许请求当前源(域名.协议.端口)的资源.如果要进行跨域请求,我们可以通过使用 html的script ...

  9. Linq/List/Array/IEnumerable等集合操作

    来源:http://www.cnblogs.com/liushanshan/archive/2011/01/05/1926263.html 目录 1    LINQ查询结果集    1 2    Sy ...

  10. ORA-25153: Temporary Tablespace is Empty解决方法

    SQL> @/tmp/4.txt create table huang_1 (deptno number,dname varchar2(19),loc varchar2(20)) * ERROR ...