POJ3189:Steady Cow Assignment(二分+二分图多重匹配)

Steady Cow Assignment

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7482   Accepted: 2572

题目链接:http://poj.org/problem?id=3189

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.

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.

题意:

每个奶牛对与每个牛棚都有个心中的排序,然后给牛选定牛棚,有两个要求,一是不超过牛棚的最大容量,二是范围尽量小。这个范围指的是给牛安排牛棚,这个牛棚在他们心中的位置,然后对于所有牛的这个位置的最小到最大。

 

题解:

给牛安排牛棚,牛棚可以容纳不止一头牛,可以看出一个二分图多重匹配。然后题目要求范围尽量小,所以我们可以二分这个范围,毕竟范围大小是有单调性的。

 

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mem(x) memset(x,0,sizeof(x))
using namespace std;

const int N = 1005 , M = 25;
int n,b,mid;
int vy[M],ylink[M][N],link[N][M],c[M],check[M];

inline int dfs(int x,int l,int r){
    for(int i=l;i<=r;i++){
        int u = link[x][i];
        if(!check[u]){
            check[u]=1;
            if(vy[u]<c[u]){
                ylink[u][++vy[u]]=x;
                return 1;
            }
            for(int j=1;j<=vy[u];j++){
                int now = ylink[u][j];
                if(dfs(now,l,r)){
                    ylink[u][j]=x;
                    return 1;
                }
            }
        }
    }
    return 0;
}

inline int Check(int range){
    bool flag ;
    for(int i=1;i+range-1<=b;i++){
        mem(vy);mem(ylink);    
        flag=true ;
        for(int j=1;j<=n;j++){
            mem(check);
            if(!dfs(j,i,i+range-1)){
                flag=false;
                break ;
            }
        }
        if(flag) return 1;
    }
    return flag;
}

int main(){
    scanf("%d%d",&n,&b);;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=b;j++) scanf("%d",&link[i][j]);
    for(int i=1;i<=b;i++) scanf("%d",&c[i]);
    int l=1,r=b<<1,Ans;
    while(l<=r){
        mid=l+r>>1;
        if(Check(mid)){        
            r=mid-1;Ans=mid;
        }else l=mid+1;
    }
    printf("%d",Ans);
    return 0;
}

 

posted @ 2018-11-15 19:02  heyuhhh  阅读(142)  评论(0编辑  收藏  举报