Steady Cow Assignment
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6728 | Accepted: 2308 |
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.
Source
题意:有n头牛和B个栏,每头牛i对每个栏j有喜爱程度rank[i][j],矩阵第i行第j列的值表示的是奶牛i会第j个中意的牛棚,每个栏i有容量cap[i],现在要把牛赶到栏里,在保证每个栏内的牛数量不超过容量的情况下,求所有牛中最大喜爱程度和最小喜爱程度差值的最小值。
解题思路:枚举所有牛的最大喜爱程度high和最小喜爱程度low,对牛的集合和栏的集合进行二分图多重匹配(一个栏可以对应多头牛)。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <bitset>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int nx,ny;
int y[1005][1005];
int visit[1005];
int sumy[1005];
int mp[1005][25];
int cap[35];
bool path(int u,int l,int r)
{
for(int i=1;i<=ny;i++)
{
if(!visit[i]&&mp[u][i]>=l&&mp[u][i]<=r)
{
visit[i]=1;
if(sumy[i]<cap[i])
{
y[i][sumy[i]++]=u;
return 1;
}
for(int j=0;j<sumy[i];j++)
{
if(path(y[i][j],l,r))
{
y[i][j]=u;
return 1;
}
}
}
}
return 0;
}
int MaxMatch(int l,int r)
{
int ans=0;
memset(sumy,0,sizeof sumy);
for(int i=1;i<=nx;i++)
{
memset(visit,0,sizeof visit);
if(path(i,l,r)) ans++;
else break;
}
return ans;
}
int main()
{
while(~scanf("%d %d",&nx,&ny))
{
int x;
for(int i=1;i<=nx;i++)
{
for(int j=ny;j>=1;j--)
{
scanf("%d",&x);
mp[i][x]=j;
}
}
for(int i=1;i<=ny;i++) scanf("%d",&cap[i]);
int l=1,r=1,ans=INF;
while(l<=r&&r<=ny)
{
if(MaxMatch(l,r)>=nx) {ans=min(r-l+1,ans);l++;}
else r++;
}
printf("%d\n",ans);
}
return 0;
}