# Poj 3189 Steady Cow Assignment【二分+多重匹配】

489人阅读 评论(1)

 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6589 Accepted: 2260

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.

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.

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.

Source

USACO 2006 February Gold

1、因为b是一个比较小的数据范围，那么我们其实可以直接两层for枚举左右区间，确定没头牛可以选择的第几喜欢的牛棚需要在l，r之间。然后进行多重匹配判定，如果所有牛都能匹配到牛棚，那么当前区间就是一个可行区间。维护记录最小值即可。

2、这里可以有一个二分查找的优化，没加优化是260+ms，加了优化之后大约60+ms。

Ac代码：

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int vis[1500];
int a[1500][50];
int match[1500][50];
int num[1500];
int c[1500];
int n,m;
int find(int u,int l,int r)
{
for(int i=l;i<=r;i++)
{
int v=a[u][i];
if(vis[v]==1)continue;
vis[v]=1;
if(num[v]<c[v])
{
match[v][num[v]]=u;
num[v]++;
return 1;
}
else
{
for(int j=0;j<c[v];j++)
{
if(find(match[v][j],l,r)==1)
{
match[v][j]=u;
return 1;
}
}
}
}
return 0;
}
int Slove(int l,int r)
{
int flag=0;
memset(match,-1,sizeof(match));
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(find(i,l,r)==1)continue;
else
{
flag=1;break;
}
}
if(flag==1)return 0;
else return 1;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<=m;i++)
{
scanf("%d",&c[i]);
}
int ans=0x3f3f3f3f;
for(int i=1;i<=m;i++)
{
int l=i;
int r=m;
int mid;
while(r-l>=0)
{
int mid=(l+r)/2;
if(Slove(i,mid)==1)
{
r=mid-1;
ans=min(ans,(mid-i+1));
}
else
{
l=mid+1;
}
}
}
printf("%d\n",ans);
}
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：494083次
• 积分：23889
• 等级：
• 排名：第271名
• 原创：1934篇
• 转载：5篇
• 译文：0篇
• 评论：196条
蒟蒟们♪(^∇^*)
阅读排行
最新评论