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

 
 

题意: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 }
View Code

 附上有注释的代码:

  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 }
View Code

 

 

 

 

转载于:https://www.cnblogs.com/UniqueColor/p/4771838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 智慧社区背景与挑战 随着城市化的快速发展,社区面临健康、安全、邻里关系和服务质量等多方面的挑战。华为技术有限公司提出智慧社区解决方案,旨在通过先进的数字化技术应对这些问题,提升城市社区的生活质量。 2. 技术推动智慧社区发展 技术进步,特别是数字化、无线化、移动化和物联化,为城市社区的智慧化提供了可能。这些技术的应用不仅提高了社区的运行效率,也增强了居民的便利性和安全性。 3. 智慧社区的核心价值 智慧社区承载了智慧城市的核心价值,通过全面信息化处理,实现对城市各个方面的数字网络化管理、服务与决策功能,从而提升社会服务效率,整合社会服务资源。 4. 多层次、全方位的智慧社区服务 智慧社区通过构建和谐、温情、平安和健康四大社区模块,满足社区居民的多层次需求。这些服务模块包括社区医疗、安全监控、情感沟通和健康监测等。 5. 智慧社区技术框架 智慧社区技术框架强调统一平台的建设,设立数据中心,构建基础网络,并通过分层建设,实现平台能力及应用的可持续成长和扩展。 6. 感知统一平台与服务方案 感知统一平台是智慧社区的关键组成部分,通过统一的RFID身份识别和信息管理,实现社区服务的智能化和便捷化。同时,提供社区内外监控、紧急救助服务和便民服务等。 7. 健康社区的构建 健康社区模块专注于为居民提供健康管理服务,通过整合医疗资源和居民接入,实现远程医疗、慢性病管理和紧急救助等功能,推动医疗模式从治疗向预防转变。 8. 平安社区的安全保障 平安社区通过闭路电视监控、防盗报警和紧急求助等技术,保障社区居民的人身和财产安全,实现社区环境的实时监控和智能分析。 9. 温情社区的情感沟通 温情社区着重于建立社区居民间的情感联系,通过组织社区活动、一键呼叫服务和互帮互助平台,增强邻里间的交流和互助。 10. 和谐社区的资源整合 和谐社区作为社会资源的整合协调者,通过统一接入和身份识别,实现社区信息和服务的便捷获取,提升居民生活质量,促进社区和谐。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值