POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)相关的知识,希望对你有一定的参考价值。
解题报告
http://blog.csdn.net/juncoder/article/details/38340447
题意:
B个猪圈,N头猪。每头猪对每一个猪圈有一个惬意值。要求安排这些猪使得最大惬意和最小惬意的猪差值最小
思路:
二分图的多重匹配问题;
猪圈和源点连边,容量为猪圈容量。猪与汇点连边,容量1;
猪圈和猪之间连线取决所取的惬意值范围;
二分查找惬意值最小差值的范围。
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #define inf 99999999 using namespace std; int n,m,b,mmap[1030][22],edge[1030][1030],l[1030],c[22]; int bfs() { memset(l,-1,sizeof(l)); l[0]=0; int i; queue<int >Q; Q.push(0); while(!Q.empty()) { int u=Q.front(); Q.pop(); for(i=0; i<=m; i++) { if(edge[u][i]&&l[i]==-1) { l[i]=l[u]+1; Q.push(i); } } } if(l[m]>1)return 1; return 0; } int dfs(int x,int f) { if(x==m)return f; int i,a; for(i=0; i<=m; i++) { if(l[i]==l[x]+1&&edge[x][i]&&(a=dfs(i,min(f,edge[x][i])))) { edge[x][i]-=a; edge[i][x]+=a; return a; } } l[x]=-1; return 0; } int dinic() { int ans=0,a; while(bfs()) while(a=dfs(0,inf)) ans+=a; return ans; } int cow(int mid) { int i,j,k; for(i=1; i<=b-mid+1; i++) { memset(edge,0,sizeof(edge)); for(j=1; j<=b; j++) { edge[0][j]=c[j]; } for(j=1; j<=n; j++) { for(k=i; k<=i+mid-1; k++) { edge[mmap[j][k]][j+b]=1; } edge[j+b][m]=1; } if(dinic()==n) return 1; } return 0; } int main() { int i,j; while(~scanf("%d%d",&n,&b)) { memset(mmap,0,sizeof(mmap)); memset(c,0,sizeof(c)); m=n+b+1; for(i=1; i<=n; i++) { for(j=1; j<=b; j++) { scanf("%d",&mmap[i][j]); } } for(i=1; i<=b; i++) { scanf("%d",&c[i]); } int l=1,r=b,t=-1; while(l<=r) { int mid=(l+r)/2; if(cow(mid)) { t=mid; r=mid-1; } else { l=mid+1; } } printf("%d\n",t); } return 0; }
Steady Cow Assignment
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5369 | Accepted: 1845 |
Description
Farmer John‘s N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of course, have limited capacity. Some cows really like their current barn, and some are not so happy.
FJ would like to rearrange the cows such that the cows are as equally happy as possible, even if that means all the cows hate their assigned barn.
Each cow gives FJ the order in which she prefers the barns. A cow‘s happiness with a particular assignment is her ranking of her barn. Your job is to find an assignment of cows to barns such that no barn‘s capacity is exceeded and the size of the range (i.e., one more than the positive difference between the the highest-ranked barn chosen and that lowest-ranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible.
FJ would like to rearrange the cows such that the cows are as equally happy as possible, even if that means all the cows hate their assigned barn.
Each cow gives FJ the order in which she prefers the barns. A cow‘s happiness with a particular assignment is her ranking of her barn. Your job is to find an assignment of cows to barns such that no barn‘s capacity is exceeded and the size of the range (i.e., one more than the positive difference between the the highest-ranked barn chosen and that lowest-ranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible.
Input
Line 1: Two space-separated integers, N and B
Lines 2..N+1: Each line contains B space-separated integers which are exactly 1..B sorted into some order. The first integer on line i+1 is the number of the cow i‘s top-choice barn, the second integer on that line is the number of the i‘th cow‘s second-choice barn, and so on.
Line N+2: B space-separated integers, respectively the capacity of the first barn, then the capacity of the second, and so on. The sum of these numbers is guaranteed to be at least N.
Lines 2..N+1: Each line contains B space-separated integers which are exactly 1..B sorted into some order. The first integer on line i+1 is the number of the cow i‘s top-choice barn, the second integer on that line is the number of the i‘th cow‘s second-choice barn, and so on.
Line N+2: B space-separated integers, respectively the capacity of the first barn, then the capacity of the second, and so on. The sum of these numbers is guaranteed to be at least N.
Output
Line 1: One integer, the size of the minumum range of barn rankings the cows give their assigned barns, including the endpoints.
Sample Input
6 4 1 2 3 4 2 3 1 4 4 2 3 1 3 1 2 4 1 3 4 2 1 4 2 3 2 1 3 2
Sample Output
2
Hint
Explanation of the sample:
Each cow can be assigned to her first or second choice: barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, and barn 4 gets cows 3 and 6.
Each cow can be assigned to her first or second choice: barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, and barn 4 gets cows 3 and 6.
以上是关于POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)的主要内容,如果未能解决你的问题,请参考以下文章
POJ3189 Steady Cow Assignment(二分图多重匹配)
POJ3189 Steady Cow Assignment(最大流)