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. 升级Android ADT 和SDK

    因为眼下从事android开发工作,所以升级了下Android SDK和eclipse ADT插件 一.更新ADT 1.Eclipse中打开Help->Install New Software. ...

  2. android手机内存大小获取

    //获取手机中文件管理器中存储大小 File datadir = Environment.getExternalStorageDirectory(); //获取手机内部的存储大小 File datad ...

  3. [Angular 2]ng-class and Encapsulated Component Style2

    Many Components require different styles based on a set of conditions. Angular 2 helps you style you ...

  4. 十二.200多万元得到的创业教训--app名字是关键

    摘要:当完毕了一个app后,就要须要上应用市场,以下讲一下起名和上应用市场的一些技巧. 健生干货分享:第12篇 1.必须是先上app store,再上其它应用市场 为啥要这样做?由于app store ...

  5. linux od命令

    用户通常使用od命令查看特殊格式的文件内容.通过指定该命令的不同选项可以以十进制.八进制.十六进制和ASCII码来显示文件.od命令系统默认的显示方式是八进制,这也是该命令的名称由来(Octal Du ...

  6. oracle 配置服务端

    oracle 配置服务端,类似我们配置java环境一样 防止乱码的配置: 变量名:NLS_LANG 变量值:SIMPLIFIED CHINESE_CHINA.ZHS16GBK 选择数据库的配置(重要) ...

  7. 图片变换 矩阵 Bitmap Matrix

    Matrix矩阵介绍 在Android中,对图片的处理需要使用到Matrix类,Matrix是一个3 x 3的矩阵,内部就是个一维数组,内部有9个元素:可以通过setValues( float[])进 ...

  8. less样式表

    LESS是一种由Alexis Sellier设计的动态层叠样式表语言.受Sass所影响,同时也影响了Sass的新语法:SCSS.[2]  LESS是开源的,其第一个版本由Ruby写成,但在后续的版本当 ...

  9. SqlServer跨域查询

    SELECT * FROM OPENDATASOURCE('SQLOLEDB','Data Source=192.168.1.14;User ID=sa;Password=sql.com').eBui ...

  10. C# 导出word文档及批量导出word文档(4)

          接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用 ...