poj 2112 Optimal Milking (二分图匹配的多重匹配)

时间:2021-02-07 14:47:28

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 ;
}