Steady Cow Assignment ——二分图多重最大匹配+折半枚举

题目链接:http://poj.org/problem?id=3189

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.

题目翻译:

农民约翰的N(1 <= N <= 1000)奶牛分别居住在B(1 <= B <= 20)谷仓之一,当然,这些谷仓的容量有限。有些奶牛真的很喜欢他们目前的谷仓,有些也不太快乐。‎
‎ FJ希望重新排列奶牛,使奶牛尽可能快乐,即使这意味着所有的奶牛都讨厌他们分配的‎
‎谷仓。‎
‎每头牛给FJ的顺序,她更喜欢‎
‎谷仓。一头奶牛的幸福是她对谷仓的排名。你的工作是找到奶牛到谷仓的分配,这样没有谷仓的容量和范围的大小(即,一个比排名最高的谷仓选择和排名最低的谷仓选择之间的正差)的谷仓排名奶牛给他们分配的谷仓是尽可能小。‎

‎输入‎

‎第 1 行:两个空间分隔的整数,N 和 B‎

‎行 2.N_1:每行包含 B 空间分隔的整数,这些整数正好是 1.B 按某个顺序排序。i+1 行上的第一个整数是牛 i 的首选谷仓的数量,该行上的第二个整数是 i'th cow 的第二选择谷仓的编号,等等。‎
‎行 N_2:B 空间分隔整数,分别位于第一个谷仓的容量、第二个谷仓的容量,‎
‎等等。这些数字的总和保证至少为 N。‎

‎输出‎

‎第 1 行:一个整数,奶牛为其分配的谷仓(包括端点)分配的谷仓排名的最小范围的大小

 

比较好的题,我们可以二分枚举区间,然后判断区间是否符合,最后输出最小范围,注意,这个题的坑点在于输入,数据读入的时候maps[i][j]并不是i_th奶牛对j_th棚子的喜爱值,而是i_th奶牛对maps[i][j]棚子的喜爱程度为j。也可以用Dinic算法来做。

#include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
int n,b;
int G[1007][23];
int visited[23],cnt[23],match[23][1007];
int cap[23];
int s,e;
int find(int x){
	for(int i = 1;i<=b;++i){
		if(G[x][i]<e&&!visited[i]&&s<=G[x][i]){
			visited[i]=1;
			if(cnt[i]<cap[i]){
				match[i][cnt[i]++]=x;
				return 1;
			}
			else{
				for(int j = 0;j<cnt[i];++j){
					if(find(match[i][j])){
						match[i][j]=x;
						return 1;
					}
				}
			}
		}
	}
	return 0;
}
int judge(int mid){
	for(s = 1;s<=b+1-mid;++s){
		int ans=0;
		e = s+mid;
		memset(cnt,0,sizeof(cnt));
		for(int i = 1;i<=n;++i){
			memset(visited,0,sizeof(visited));
			if(find(i)) ans++;
		}
		if(ans==n) return 1;
	}
	return 0;
}
int main(int argc, char** argv) {
	while(cin>>n>>b){
		memset(G,0,sizeof(G));
		for(int i = 1;i<=n;++i){
			for(int j = 1;j<=b;++j){
				int x;
				cin>>x;
				G[i][x]=j;
			}
		}
		for(int i = 1;i<=b;++i) cin>>cap[i];
		int l=1,r=b,minn=0;
		while(l<=r){
			int mid=(l+r)/2;
			if(judge(mid)){
				minn=mid;
				r=mid-1;
			}
			else l=mid+1;
		} 
    	cout<<minn<<endl;
	}

	return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值