[kuangbin] POJ3189 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

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.


题目大意:每个牛对于每个牛棚都有一个喜好值,现在要重新分配牛棚,问怎样分配牛,才可以使所有牛的喜好值 在一个最小的区间呢.

题目中有两个坑:

第一 mp[i][j]不代表是 i牛对j牛棚的喜好值,代表i牛对mp[i][j]牛棚的喜好值为j

第二 求的是区间长度 而不是区间最大值-最小值

题目思路:

首先很明显,是个二分的题目,二分当前这个区间长度,看在这个区间长度内 ,是否可以把牛都分配完.

怎样二分这个区间长度?

我们假设当前区间长度为 mid

因为喜好值 最大 为 m ,所以我们没救 [i,i+mid-1]这个区间,只有在这个区间的喜好值 这个牛才可以选这个牛棚,就这样跑一遍多重匹配,如果检测成功,就变小,否则变大;

AC:

//#include<bits/stdc++.h>
#include<queue>
#include<algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
const int maxn=1505;
const ll INF=1e9+7;
inline ll read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
ll n,m,p;
vector<int>v[1500];
char str[1500];
bool used[1500];
vector<int>nR[1500];
bool dfs(int x,int limit)
{
    int sz=v[x].size();
    for(int i=0;i<sz;i++)
    {
        int e=v[x][i];
        if(!used[e])
        {
            used[e]=true;
            int sz=nR[e].size();
            if(sz<limit)
            {
                nR[e].push_back(x);
                return true;
            }
            else
            {
                for(int k=0;k<sz;k++)
                {
                    if(dfs(nR[e][k],limit))
                    {
                        nR[e][k]=x;
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
bool judge(int x)
{
    for(int i=1;i<=n;i++)
    {
        memset(used,false,sizeof(used));
        if(!dfs(i,x)) return false;
    }
    return true;
}
ll AC()
{
    ll ans=0;
    ll l=1,r=n;
    while(l<=r)
    {
        for(int i=0;i<=m;i++) nR[i].clear();
        ll mid=(l+r)/2;
        if(judge(mid))
        {
            ans=mid;
            r=mid-1;
        }
        else
            l=mid+1;
    }
    return ans;
}
int main()
{
    while(scanf("%lld%lld",&n,&m),n+m)
    {
        for(int i=0;i<=n;i++) v[i].clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",str);
            while(getchar()!='\n')
            {
               int x;scanf("%d",&x);
               v[i].push_back(x);
            }
        }
        printf("%lld\n",AC());
    }
    return 0;
}

总结:

1.二分区间长度可以通过枚举!!

上一篇:P3033 [USACO11NOV]牛的障碍Cow Steeplechase


下一篇:Cow Contest POJ - 3660 Floyd算法,关系链图