poj 3189(枚举+最大流)

来源:互联网 发布:js类数组对象转为数组 编辑:程序博客网 时间:2024/04/29 08:35
Steady Cow Assignment
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3533 Accepted: 1214

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
题目:http://poj.org/problem?id=3189
分析:这题要求最小的分数区间,满足所有牛能住下,我们可以枚举区间长度和区间分数,把在该区间内的牛和牛棚连上边,牛和源连上边,牛棚和汇连上边,最大流判断是否满足要求。。。
代码:
#include<cstdio>using namespace std;const int mm=222222;const int mn=2222;const int oo=1000000000;int node,src,dest,edge;int ver[mm],flow[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn];int level[mn][22],cap[22],n,m;inline int min(int a,int b){    return a<b?a:b;}inline void prepare(int _node,int _src,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0;i<node;++i)head[i]=-1;    edge=0;}inline void addedge(int u,int v,int c){    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){    int i,u,v,l,r=0;    for(i=0;i<node;++i)dis[i]=-1;    dis[q[r++]=src]=0;    for(l=0;l<r;++l)        for(i=head[u=q[l]];i>=0;i=next[i])            if(flow[i]&&dis[v=ver[i]]<0)            {                dis[q[r++]=v]=dis[u]+1;                if(v==dest)return 1;            }    return 0;}int Dinic_dfs(int u,int exp){    if(u==dest)return exp;    for(int &i=work[u],v,tmp;i>=0;i=next[i])        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            return tmp;        }    return 0;}int Dinic_flow(){    int i,ret=0,delta;    while(Dinic_bfs())    {        for(i=0;i<node;++i)work[i]=head[i];        while(delta=Dinic_dfs(src,oo))ret+=delta;    }    return ret;}int solve(){    int ans,i,j,k;    for(ans=1;ans<=m;++ans)    {        for(k=1;k+ans-1<=m;++k)        {            prepare(n+m+2,0,n+m+1);            for(i=1;i<=n;++i)addedge(src,i,1);            for(i=1;i<=m;++i)addedge(i+n,dest,cap[i]);            for(i=1;i<=n;++i)                for(j=k;j<k+ans;++j)addedge(i,level[i][j]+n,1);            if(Dinic_flow()==n)return ans;        }    }    return m;}int main(){    int i,j;    while(scanf("%d%d",&n,&m)!=-1)    {        for(i=1;i<=n;++i)            for(j=1;j<=m;++j)scanf("%d",&level[i][j]);        for(i=1;i<=m;++i)scanf("%d",&cap[i]);        printf("%d\n",solve());    }    return 0;}