• POJ3189 Steady Cow Assignment


    Steady Cow Assignment

    Language:
    Steady Cow Assignment
    Time Limit: 1000MSMemory Limit: 65536K
    Total Submissions: 7593Accepted: 2616

    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

    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

    N头牛M个棚,每头牛对每个棚都有喜爱顺序,每个棚都有自己的容量
    问你怎么分配使得所有牛的喜爱程度差距最小

    题解

    二分答案,扫描建边,跑最大流来做多重匹配判断可行性。

    时间复杂度(O(sqrt{n+m}nm*mlog m))

    #include<iostream>
    #include<cstring>
    #include<queue>
    #define rg register
    #define il inline
    #define co const
    template<class T>il T read(){
        rg T data=0,w=1;rg char ch=getchar();
        for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
        for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
        return data*w;
    }
    template<class T>il T read(rg T&x) {return x=read<T>();}
    typedef long long ll;
    using namespace std;
    
    co int N=1e3+1,M=21;
    int n,m,a[N][M],c[M],ans;
    int head[N+M],edge[N*M*2],leng[N*M*2],next[N*M*2],tot;
    int d[N+M];
    
    il void add(int x,int y,int z){
    	edge[++tot]=y,leng[tot]=z,next[tot]=head[x],head[x]=tot;
    }
    bool bfs(){
    	memset(d,0,sizeof d),d[0]=1;
    	queue<int> q;q.push(0);
    	while(q.size()){
    		int x=q.front();q.pop();
    		for(int i=head[x];i;i=next[i]){
    			int y=edge[i],z=leng[i];
    			if(d[y]||!z) continue;
    			d[y]=d[x]+1,q.push(y);
    		}
    	}
    	return d[n+m+1]!=0;
    }
    int dinic(int x,int flow){
    	if(x==n+m+1) return flow;
    	int rest=flow;
    	for(int i=head[x];i&&rest;i=next[i]){
    		int y=edge[i],z=leng[i];
    		if(d[y]!=d[x]+1||!z) continue;
    		int delta=dinic(y,min(rest,z));
    		if(!delta) d[y]=0;
    		else{
    			leng[i]-=delta,leng[i^1]+=delta;
    			rest-=delta;
    		}
    	}
    	return flow-rest;
    }
    bool work(int l,int r){
    	tot=1;
    	for(int i=0;i<=n+m+1;++i) head[i]=0;
    	for(int i=1;i<=n;++i)for(int j=l;j<=r;++j)
    		add(i,n+a[i][j],1),add(n+a[i][j],i,0);
    	for(int i=1;i<=n;++i) add(0,i,1),add(i,0,0);
    	for(int i=1;i<=m;++i) add(n+i,n+m+1,c[i]),add(n+m+1,n+i,0);
    	int now=0,ans=0;
    	while(bfs())
    		while((now=dinic(0,0x7fffffff))) ans+=now;
    	return ans==n;
    }
    bool pd(int now){
    	for(int l=1;l+now-1<=m;++l)
    		if(work(l,l+now-1)) return 1;
    	return 0;
    }
    int main(){
    	read(n),read(m);
    	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j) read(a[i][j]);
    	for(int i=1;i<=m;++i) read(c[i]);
    	int l=1,r=m;
    	while(l<r){
    		int mid=l+r>>1;
    		if(pd(mid)) r=mid;
    		else l=mid+1;
    	}
    	printf("%d
    ",l);
    	return 0;
    }
    
  • 相关阅读:
    Spring学习(21)--- AOP之Advice应用(上)
    Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut
    Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect
    Spring学习(18)--- AOP基本概念及特点
    Spring学习(17)--- 三种装配Bean方式比较
    Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)
    Spring学习(15)--- 基于Java类的配置Bean 之 @Bean & @Scope 注解
    怎么找到与你Eclipse匹配的spring tool suite插件
    eclipse安装hibernate插件(在线Marketplace中安装)
    从配置maven环境到maven项目的新建
  • 原文地址:https://www.cnblogs.com/autoint/p/11050294.html
Copyright © 2020-2023  润新知