# POJ 2112 Optimal Milking, 二分, floyd, 二分图

http://poj.org/problem?id=2112

Optimal Milking

TimeLimit: 2000MS           MemoryLimit: 30000K

Case TimeLimit: 1000MS

Input

* Line 1: A single line withthree space-separated integers: K, C, and M.

* Lines 2.. ...: Each of these K+C lines of K+C space-separated integersdescribes the distances between pairs of various entities. The input forms asymmetric matrix. Line 2 tells the distances from milking machine 1 to each ofthe other entities; line 3 tells the distances from machine 2 to each of theother entities, and so on. Distances of entities directly connected by a pathare positive integers no larger than 200. Entities not directly connected by apath have a distance of 0. ...(后面的废话省略)

Output

A single line with a singleinteger that is the minimum possible total distance for the furthest walkingcow.

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

Source

USACO2003 U S Open

596K 297MS

<span style="font-size:18px;">/*
floyd,二分答案，二分图匹配。简洁明了
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int inf=250,Max=0x3f3f3f3f;

int dis[inf][inf];

//二分图匹配：
bool map[30*15+10][inf];//将每个机器拆成m个点存储。map[i][j]表示第i个机器到第j头牛之间的边
bool vis[30*15+10];
int use[30*15+10];

int k,c,m,n;

void floyd(){
for (int r=1;r<=n;r++)
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
dis[i][j]=min(dis[i][j],dis[i][r]+dis[r][j]);
}

void buildmap(int maxd){		//建二分图
memset(map,false,sizeof(map));

for (int i=1;i<=k;i++)
for (int j=k+1;j<=n;j++)
if (dis[i][j]<=maxd)
for (int r=0;r<m;r++)	//把每个机器拆成m个
map[r*k+i][j-k]=true;
}

bool dfs(int i){
for (int j=1;j<=k*m;j++){
if (map[j][i] && !vis[j]){
vis[j]=true;
if (!use[j] || dfs(use[j])){
use[j]=i;
return true;
}
}
}
return false;
}
bool match(){		//增广路求二分图匹配
memset(use,0,sizeof(use));
for (int i=1;i<=c;i++){
memset(vis,false,sizeof(vis));
if (!dfs(i)) return false;		//只要有一个点不能匹配就算建图失败
}
return true;
}

int main(){
cin>>k>>c>>m;	n=k+c;
for (int i=1;i<=n;i++) for (int j=1;j<=n;j++){
scanf("%d",&dis[i][j]);
if (!dis[i][j]) dis[i][j]=Max;
}

floyd();

//二分答案
int l=0,r=200*n,ans;
while(l<=r){
int mid=(l+r)>>1;
buildmap(mid);			//建二分图：二分图中只保留长度小于等于mid的边
if (match()) r=mid-1,ans=mid;		//能够匹配说明：最长边的最小值不够小
else l=mid+1;
}

cout<<ans;
return 0;
} </span>

10-08 611

07-19 2457

08-03 48

04-15 342

11-15 14

08-26 20

07-25 352

08-08 472

05-27 1885

06-21 23

#### POJ 2112 Optimal Milking （floyd+二分+二分图多重匹配）

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。