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 }
附上有注释的代码:
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