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. (转)Maven实战(二)构建简单Maven项目

    上一节讲了maven的安装和配置,这一节我们来学习一下创建一个简单的Maven项目 1. 用Maven 命令创建一个简单的Maven项目 在cmd中运行如下命令: mvn archetype:gene ...

  2. MAC安装SVNServer

    MAC已经自带了SVN,所以,直接使用就好 1.创建svn repository svnadmin create /path/svn/pro  //仓库位置,svn是svn的目录,pro是一个版本库的 ...

  3. Java基础知识强化58:经典排序之二叉树排序(BinaryTreeSort)

    1. 二叉树排序 二叉树排序的描述也是一个递归的描述, 所以二叉树排序的构造自然也用递归的: 二叉排序树或者是一棵空树,或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它 ...

  4. Shell防DDOS攻击原理

    #!/bin/sh #date:2015-12-13 #filename:fang-DOS.sh  #version:v1.0 while true do     #awk '{print $1}' ...

  5. 忘记mysql 5.7的密码

    for windows: http://blog.chinaunix.net/uid-27570589-id-3511820.html 一.将net stop mysql; 二.在命令行中 C:\Us ...

  6. Function.prototype.bind

    解析Function.prototype.bind 简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. b ...

  7. traditional:true

  8. 安全性良好的operator=操作,和新的new方法(针对深度复制的情况)

    class B { }; class A { public: A& operator=(const A& a) { B* temp = b; //这里解决重复赋值的方法是用temp指向 ...

  9. hdu5362 Just A String(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Just A String Time Limit: 2000/1000 MS (J ...

  10. uva 759 - The Return of the Roman Empire

    #include <cstdio> #include <string> #include <map> using namespace std; ; , , , , ...