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

Steady Cow Assignment

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

 

题目大意:有n头牛,b个牛棚,接下来一个n*b的一个矩阵,Aij表示第i头牛第j个喜欢的牛棚编号。为了公平起见,尽可能的使得每头牛的喜欢程度相近,使得最终全部牛都有了牛棚之后,使得最高兴的牛(比如是选择了第j个喜欢的牛棚)-最不高兴的牛(比如是选择了第y个喜欢的牛棚)+1尽可能的小(j-y+1)。


思路:


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


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

因为随着区间的增大,可以匹配上的牛的数量会随着增加,那么其满足递增性,那么我们一层for枚举左区间,然后二分查找一个右区间,然后进行多重匹配的判定即可。


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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值