Steady Cow Assignment POJ - 3189 二分差值最大流

Steady Cow Assignment
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions:7173 Accepted: 2470

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


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;


const int MAX_D=2000+5;
const int MAX_B=50000+5;
const int INF=0x3f3f3f3f;



int p[1005][25];
int pp[25];
int tot;
int N,B;


int head[MAX_D];
int iter[MAX_D];
int level[MAX_D];
struct node{
    int to;
    int cap;
    int rev;
    int next;
};
node edge[2*MAX_B];






void add_edge(int a,int b,int c){


    edge[tot].to=b;
    edge[tot].cap=c;
    edge[tot].rev=tot+1;
    edge[tot].next=head[a];
    head[a]=tot++;


    edge[tot].to=a;
    edge[tot].cap=0;
    edge[tot].rev=tot-1;
    edge[tot].next=head[b];
    head[b]=tot++;


}




void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(int i=head[v];i!=0;i=edge[i].next){
            node &e=edge[i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}


int dfs(int v,int t,int f){
    if(v==t)return f;
    for(int &i=iter[v];i!=0;i=edge[i].next){
        node &e = edge[i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                edge[e.rev].cap+=d;
                return d;
            }
        }
    }


    return 0;
}


int max_flow(int s,int t){
    int flow=0;
    while(1){
        bfs(s);
        if(level[t]<0)return flow;
        for(int i=0;i<MAX_D;i++){
            iter[i]=head[i];
        }
        int f;
        while((f=dfs(s,t,INF))>0){
            flow+=f;
        }
    }
}

bool ok(int x){
    for(int i=1;i<=B-x+1;i++){
        tot=1;
        memset(head,0,sizeof(head));
        for(int k=1;k<=N;k++){
            add_edge(1,k+2,1);
            for(int j=i;j<=i+x-1;j++){
                add_edge(k+2,p[k][j]+N+2,1);
            }
        }
        for(int k=1;k<=B;k++){
            add_edge(k+N+2,2,pp[k]);
        }


        if(max_flow(1,2)==N){
            return true;
        }
    }

    return false;

}



void solve(){

    while(~scanf("%d%d",&N,&B)){
        for(int i=1;i<=N;i++){
            for(int j=1;j<=B;j++){
                scanf("%d",&p[i][j]);
            }
        }
        for(int i=1;i<=B;i++){
            scanf("%d",&pp[i]);
        }

        int left=1,right=B,ans=-1;

        while(left<=right){
            int mid=(left+right)>>1;

            if(ok(mid)){
                ans=mid;
                right=mid-1;
            }
            else{
                left=mid+1;
            }
        }
        printf("%d\n",ans);

    }


}


int main()
{
    solve();


    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值