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

Description

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

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

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

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

USACO 2003 U S Open

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

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

1、首先floyd求出最短距离

2、二分答案, 重新建图,把多重匹配的点分裂成多个点来解二分图的最大匹配

3、看看二分的答案是否符合全部牛的匹配情况,然后继续二分

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 #define N 206
 8 #define inf 1<<29
 9 int k,c,m;
10 int mp[236][236];
11 int path[N][7000];
12 int match[7000];
13 int vis[7000];
14 void flyod(){
15     for(int L=1;L<=k+c;L++){
16         for(int i=1;i<=k+c;i++){
17             for(int j=1;j<=k+c;j++){
18                 if(mp[i][j]>mp[i][L]+mp[L][j]){
19                     mp[i][j]=mp[i][L]+mp[L][j];
20                 }
21             }
22         }
23     }
24 }
25 void changePath(int mid){
26     for(int i=1;i<=c;i++){
27         for(int j=1;j<=k;j++){
28             if(mp[k+i][j]<=mid){
29                 for(int t=1;t<=m;t++){
30                     path[i][(j-1)*m+t]=1;
31                 }
32             }
33         }
34     }
35 }
36 bool dfs(int x){
37     for(int i=1;i<=k;i++){
38         for(int j=1;j<=m;j++){
39             int u=(i-1)*m+j;
40             if(path[x][u] && !vis[u]){
41                 vis[u]=1;
42                 if(match[u]==-1 || dfs(match[u])){
43                     match[u]=x;
44                     return true;
45                 }
46             }
47         }
48     }
49     return false;
50 }
51 bool judge(){
52
53     memset(match,-1,sizeof(match));
54     for(int i=1;i<=c;i++){
55         memset(vis,0,sizeof(vis));
56         if(!dfs(i)){
57             return false;
58         }
59     }
60     return true;
61
62 }
63 void solve(){
64     int L=0,R=1000000;
65     while(L<R){
66         int mid=(L+R)>>1;
67         memset(path,0,sizeof(path));
68         changePath(mid);
69         if(judge()){
70             R=mid;
71         }else{
72             L=mid+1;
73         }
74     }
75     printf("%d\n",L);
76 }
77 int main()
78 {
79     while(scanf("%d%d%d",&k,&c,&m)==3){
80
81         for(int i=1;i<=k+c;i++){
82             for(int j=1;j<=k+c;j++){
83                 scanf("%d",&mp[i][j]);
84                 if(mp[i][j]==0){
85                     mp[i][j]=inf;
86                 }
87             }
88         }
89
90         flyod();
91         solve();
92     }
93     return 0;
94 }

 附上有注释的代码:

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <algorithm>
  6 #include <cmath>
  7
  8 const int MAXK = 30 + 1;
  9 const int MAXC = 200 + 1;
 10 const int MAXM = 15 + 1;
 11 const int INF  = 100000000;
 12
 13 using namespace std;
 14
 15 int  k, c, m;
 16 int  map[MAXK+MAXC][MAXK+MAXC];
 17 bool path[MAXC][MAXK*MAXM];
 18 int  match[MAXK*MAXM];
 19 bool vst[MAXK*MAXM];
 20
 21
 22 /* 把每个挤奶器点分裂成 m 个点,选边权 <=tmp 的边建立二分图 */
 23 void buildGraph(int tmp)
 24 {
 25     memset(path, false, sizeof(path));
 26
 27     for (int i=1; i<=c; i++)
 28         for (int j=1; j<=k; j++)
 29             if (map[k+i][j] <= tmp)
 30             {
 31                 for (int t=1; t<=m; t++)
 32                 {
 33                     path[i][(j-1)*m+t] = true;
 34                 }
 35             }
 36 }
 37
 38 bool DFS(int i)
 39 {
 40     for (int j=1; j<=k*m; j++)
 41     {
 42         if (path[i][j] && !vst[j])
 43         {
 44             vst[j] = true;
 45             if (match[j] == -1 || DFS(match[j]))
 46             {
 47                 match[j] = i;
 48                 return true;
 49             }
 50         }
 51     }
 52     return false;
 53 }
 54
 55 /* 针对该题,做了小小的修改,全部匹配返回 true, 否则返回 false */
 56 bool maxMatch()
 57 {
 58     memset(match, -1, sizeof(match));
 59     for (int i=1; i<=c; i++)
 60     {
 61         memset(vst, false, sizeof(vst));
 62         if (!DFS(i))
 63             return false;
 64     }
 65     return true;
 66 }
 67
 68 /*  二分答案,求二分图最大匹配  */
 69 void solve()
 70 {
 71     int low = 1, high = 200*(k+c), mid;
 72     while (low < high)
 73     {
 74         mid = (low + high)/2;
 75         buildGraph(mid);
 76         maxMatch() == true ? high = mid : low = mid+1;
 77     }
 78     printf("%d\n", low);
 79 }
 80
 81 void floyd()
 82 {
 83     int i, j, h, t = k+c;
 84     for (h=1; h<=t; h++)
 85         for (i=1; i<=t; i++)
 86             for (j=1; j<=t; j++)
 87                 if (map[i][j] > map[i][h]+map[h][j])
 88                     map[i][j] = map[i][h]+map[h][j];
 89 }
 90
 91 int main()
 92 {
 93     scanf("%d %d %d", &k, &c, &m);
 94     for (int i=1; i<=k+c; i++)
 95         for (int j=1; j<=k+c; j++)
 96         {
 97             scanf("%d", &map[i][j]);
 98             if (map[i][j] == 0)
 99                 map[i][j] = INF;
100         }
101     floyd();
102     solve();
103     return 0;
104 }

时间: 2024-09-28 18:00:29

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

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

POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C≤200)头奶牛,在奶牛和挤奶器之间有一组不同长度的路.K个挤奶器的位置用1-K的编号标明,奶牛的位置用K+1-K+C 的编号标明.每台挤奶器每天最多能为M(1≤M≤15)头奶牛挤奶.寻找一个方案,安排每头奶牛到某个挤奶器挤奶,并使得C 头奶牛需要走的所有路程中的最大路程最小.每个测试数据中至少有一

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

POJ 2112 Optimal Milking 题目链接 题意:给定一些机器和奶牛,在给定距离矩阵,(不在对角线上为0的值代表不可达),每个机器能容纳m个奶牛,问所有奶牛都能挤上奶,那么走的距离最大的奶牛的最小值是多少 思路:明显的二分+最大流,注意floyd求出的距离矩阵最大值可能不止200,所以二分的上限要注意 代码: #include <cstdio> #include <cstring> #include <queue> #include <algori

POJ 2112 Optimal Milking 最优挤奶方案 Floyd算法+二分查找+最大流

题目链接:POJ 2112 Optimal Milking Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12446   Accepted: 4494 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among

POJ 2112 Optimal Milking 二分答案+最大流

首先二分最长的边,然后删去所有比当前枚举的值长的边,算最大流,看是否能满足所有的牛都能找到挤奶的地方 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

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

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

POJ 2112 Optimal Milking

Optimal Milking Time Limit: 2000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 211264-bit integer IO format: %lld      Java class name: Main FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures amon

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

题目大意:K台挤奶机,C个奶牛,每台挤奶器可以供M头牛使用,给出奶牛和和机器间的距离矩阵,求所有奶牛走最大距离的最小值 思路:最大距离的最小值,明显提示二分,将最小距离二分之后问题转化成为:K台挤奶机,C个奶牛,每台挤奶器可以供M头牛使用,已知每头牛可以到的挤奶机是哪些,问能否让所有奶牛挤上奶. 这个问题就是典型的二分图多重匹配问题,跑个网络流看是否满流即可,最后才发现给出的矩阵不一定是最短路径TUT 所以要跑一遍floyd #include<iostream> #include<cst

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

题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 奶的情况下,走的最远的那头奶牛走的距离最小是多少. 数据保证有解. 算法讨论: 首先可以想到是二分,然后在选择流网络的时候,一开始选择的最小费用最大流,让二分的边权充当最小费用,但是这样跑发现每次二分的是我们要跑的答案,不可行.所以就改用最大流. 最大流肯定是在二分的情况下判定最大流是否等于c,即

POJ 2112 Optimal Milking(最大流)

题目链接:http://poj.org/problem?id=2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The