POJ 3189 Steady Cow Assignment

POJ 3189 Steady Cow Assignment

题目链接

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

有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度,每个棚子也有一个最大容量了。现在要给每个奶牛分配一个棚子,使得喜爱程度差值最小。
因为每个棚子并不是住一个奶牛,所以可以考虑二分多重匹配咯。匹配的时候二分枚举喜爱程度的区间大小,根据区间大小来枚举区间的起点和终点,然后跑多重匹配判断是否合法即可。注意求得是区间大小!
还有一个坑点是数据读入,读入的是棚子的编号 idid,喜爱程度则是此时的位置,即 g[i][id]=jg[i][id]=j,不理解的可以仔细看我题面标红的部分。
AC代码如下:

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int N=1005;
int link[N][N],vis[N],num[N],g[N][N],w[N];
int m,n,id,s,e;
int found(int u){
    for(int v=1;v<=m;v++){
        if(!vis[v] && g[u][v]>=s && g[u][v]<e){
            vis[v]=1;
            if(num[v]<w[v]){
                link[v][++num[v]]=u;
                return 1;
            }
            for(int i=1;i<=num[v];i++){
                if(found(link[v][i])){
                    link[v][i]=u;
                    return 1;
                }
            }
        }
    }
    return 0;
}

int hungary(int lim){
    for(s=1;s<=m-lim+1;s++){
        fill(num,num+N,0);
        e=s+lim;
        int ans=0;
        for(int i=1;i<=n;i++){
            fill(vis,vis+N,0);
            if(found(i)) ans++;
        }
        if(ans==n) return 1;
    }
    return 0;
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        fill(g[0],g[0]+N*N,0);
        fill(vis,vis+N,0);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&id);
                g[i][id]=j;
            }
        }
        for(int i=1;i<=m;i++) scanf("%d",&w[i]);
        int l=1,r=m;
        while(l<=r){
            int mid=(l+r)>>1;
            if(hungary(mid)) r=mid-1;
            else l=mid+1;
        }
        printf("%d\n",l);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章