POJ3189 Steady Cow Assignment

来源:互联网 发布:神知结局形象 编辑:程序博客网 时间:2024/05/29 06:36
Steady Cow Assignment
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7050 Accepted: 2436

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 41 2 3 42 3 1 44 2 3 13 1 2 41 3 4 21 4 2 32 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.

一道二分图多重匹配的问题,但是题目的描述巨坑,两大坑点,第一点,输入非常坑,第二要的结果非常坑。
题意:有n个奶牛,b个棚子,每个奶牛对于棚子都有喜欢值,但是没个棚子也有居住的最高上限,现在要给这些奶牛安家,看怎么分配让奶牛中最大满意度减去最小满意度差值最小。
这个题我看以前做过一道很相似的,当时不会二分图,我就直接暴力,贪心试试,一直过不去,忘了是超时还是wa,我记得当时我枚举所有的最大最小值来做,现在学了二分图虽然也是枚举,但是枚举的是喜欢程度的区间,当然是用二分枚举的,然后再根据区间枚举最大最小值来跑多重匹配看是否合适。
最后的坑点就是输入了,以2 3 1 4为例。每行是奶牛最喜欢的是2号棚子,第二喜欢的是3号棚子,而不是第一个棚子的喜欢程度是2,第二个棚子的喜欢程度是3。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,b;int mapp[1005][25],pp[1005][25],v[1005],limit[25];bool used[1005];int s,e,ans,mid;int found(int x){    for(int i=1;i<=b;i++)    {        if(!used[i]&&mapp[x][i]<e&&s<=mapp[x][i])        {            used[i]=1;            if(v[i]<limit[i])            {                pp[i][v[i]++]=x;                return 1;            }            for(int j=0;j<v[i];j++)            {                if(found(pp[i][j]))                {                    pp[i][j]=x;                    return 1;                }            }        }    }    return 0;}int aa(){    for(int i=1;i<=b-mid+1;i++)//枚举起点,终点    {        s=i;        e=s+mid;        ans=0;        memset(v,0,sizeof(v));        for(int i=1; i<=n; i++)        {            memset(used,0,sizeof(used));            if(found(i))                ans++;        }        if(ans==n)            return 1;    }    return 0;}int main(){    while(~scanf("%d%d",&n,&b))    {        int x;        for(int i=1;i<=n;i++)        {            for(int j=b;j>=1;j--)//这个代表满意度            {                scanf("%d",&x);                mapp[i][x]=j;//先输入的肯定是最喜欢的,x号棚子满意度是b,然后下一个棚子满意度是b-1,以此类推            }        }        for(int i=1;i<=b;i++)            scanf("%d",&limit[i]);        int l=1,r=b;        while(l<=r)//枚举区间        {            mid=(l+r)/2;            if(aa())                r=mid-1;            else                l=mid+1;        }        printf("%d\n",r+1);    }    return 0;}


阅读全文
0 0