1. 程式人生 > >POJ3189:Steady Cow Assignment(二分+二分圖多重匹配)

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;
}