POJ3189 Steady Cow Assignment

来源:互联网 发布:女神联盟2进阶15数据 编辑:程序博客网 时间:2024/05/29 07:44
Steady Cow Assignment
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6817 Accepted: 2349

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 41 2 3 42 3 1 44 2 3 13 1 2 41 3 4 21 4 2 32 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.

Source

USACO 2006 February Gold

————————————————————————————

题目的意思是:有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度。棚子有最大容量了,现在要给每个奶牛安家,找一个奶牛喜爱程度差值最小的方案问喜爱程度的区间最小为多大?

思路:尺取枚举区间端点,二分图多重匹配验证

注意:2~N+1行每行的每个数x不是指i对j的喜爱程度为x而是i对x的喜爱程度为j

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int MAXN=1005;int uN,vN;  //u,v数目int g[MAXN][MAXN];int linker[MAXN][MAXN];bool used[MAXN];int linknum[MAXN];int cap[MAXN];int mp[MAXN][MAXN];int L,R;bool dfs(int u){    int v;    for(v=1; v<=vN; v++)        if(mp[u][v]<=R&&mp[u][v]>=L&&!used[v])        {            used[v]=true;            if(linknum[v]<cap[v])            {                linker[v][++linknum[v]]=u;                return true;            }            for(int i=1; i<=cap[v]; i++)                if(dfs(linker[v][i]))                {                    linker[v][i]=u;                    return true;                }        }    return false;}int hungary(){    int res=0;    int u;    memset(linknum,0,sizeof linknum);    memset(linker,-1,sizeof linker);    for(u=1; u<=uN; u++)    {        memset(used,0,sizeof used);        if(dfs(u))  res++;    }    return res;}int main(){    int n,m,k,x;    while(~scanf("%d%d",&uN,&vN))    {        for(int i=1; i<=uN; i++)            for(int j=1; j<=vN; j++)            {                scanf("%d",&x);                mp[i][x]=j;            }        for(int i=1; i<=vN; i++)            scanf("%d",&cap[i]);        L= R = 1;        int  ans = INF;        while(L <= R && R <= vN)        {            if(hungary()==uN)            {                ans=min(ans,R-L+1);                    L++;            }            else                R++;        }        printf("%d\n",ans);    }    return 0;}


原创粉丝点击