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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章