poj 3189 Steady Cow Assignment 二分图多重匹配+二分

题目:

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

思路:

建好图之后,通过二分的方式来确定区间,然后判定是否符合条件。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=1e3+5;
int a[maxn][maxn];
int match[maxn][maxn];
int vis[maxn];
int num[maxn];
int lim[maxn];
int n,b;
bool Find(int x,int l,int r)
{
    for (int i=1;i<=b;i++)
    {
        if(!vis[i]&&a[x][i]>=l&&a[x][i]<=r)
        {
            vis[i]=1;
            if(num[i]<lim[i])
            {
                match[i][num[i]++]=x;
                return true;
            }
            for (int j=0;j<num[i];j++)
            {
                if(Find(match[i][j],l,r))
                {
                    match[i][j]=x;
                    return true;
                }
            }
        }
    }
    return false;
}
bool algor(int l,int r)
{
    memset (num,0,sizeof(num));
    for (int i=1;i<=n;i++)
    {
        memset (vis,0,sizeof(vis));
        if(!Find(i,l,r)) return false;
    }
    return true;
}
int main()
{
    while(scanf("%d%d",&n,&b)!=EOF)
    {
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=b;j++)
            {
                int x;
                scanf("%d",&x);
                a[i][x]=j;
            }
        }
        for (int i=1;i<=b;i++) scanf("%d",&lim[i]);
        int l=0,r=b*b;
        while(l<r)
        {
            int mid=(l+r)/2;
            int ok=0;
            for (int i=1;i<=b;i++)
            {
                if(algor(i,i+mid))
                {
                    ok=1;
                    break;
                }
            }
            if(ok) r=mid;
            else l=mid+1;
        }
        printf("%d\n",r+1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/88383048
今日推荐