POJ3189:Steady Cow Assignment(二分+二分图多重匹配)
Steady Cow Assignment
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 7482 | Accepted: 2572 |
题目链接:http://poj.org/problem?id=3189
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.
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.
题意:
每个奶牛对与每个牛棚都有个心中的排序,然后给牛选定牛棚,有两个要求,一是不超过牛棚的最大容量,二是范围尽量小。这个范围指的是给牛安排牛棚,这个牛棚在他们心中的位置,然后对于所有牛的这个位置的最小到最大。
题解:
给牛安排牛棚,牛棚可以容纳不止一头牛,可以看出一个二分图多重匹配。然后题目要求范围尽量小,所以我们可以二分这个范围,毕竟范围大小是有单调性的。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mem(x) memset(x,0,sizeof(x))
using namespace std; const int N = , M = ;
int n,b,mid;
int vy[M],ylink[M][N],link[N][M],c[M],check[M]; inline int dfs(int x,int l,int r){
for(int i=l;i<=r;i++){
int u = link[x][i];
if(!check[u]){
check[u]=;
if(vy[u]<c[u]){
ylink[u][++vy[u]]=x;
return ;
}
for(int j=;j<=vy[u];j++){
int now = ylink[u][j];
if(dfs(now,l,r)){
ylink[u][j]=x;
return ;
}
}
}
}
return ;
} inline int Check(int range){
bool flag ;
for(int i=;i+range-<=b;i++){
mem(vy);mem(ylink);
flag=true ;
for(int j=;j<=n;j++){
mem(check);
if(!dfs(j,i,i+range-)){
flag=false;
break ;
}
}
if(flag) return ;
}
return flag;
} int main(){
scanf("%d%d",&n,&b);;
for(int i=;i<=n;i++)
for(int j=;j<=b;j++) scanf("%d",&link[i][j]);
for(int i=;i<=b;i++) scanf("%d",&c[i]);
int l=,r=b<<,Ans;
while(l<=r){
mid=l+r>>;
if(Check(mid)){
r=mid-;Ans=mid;
}else l=mid+;
}
printf("%d",Ans);
return ;
}
POJ3189:Steady Cow Assignment(二分+二分图多重匹配)的更多相关文章
- POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65 ...
- POJ3189 Steady Cow Assignment
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6817 Accepted: ...
- Poj 2289 Jamie's Contact Groups (二分+二分图多重匹配)
题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...
- [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)
http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...
- POJ2289:Jamie's Contact Groups(二分+二分图多重匹配)
Jamie's Contact Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/ ...
- POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment
这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...
- POJ3189 Steady Cow Assignment(最大流)
题目大概说,有n头牛和b块草地,每头牛心中分别对每块草地都有排名,草地在牛中排名越高牛安排在那的幸福度就越小(...),每块草地都能容纳一定数量的牛.现在要给这n头牛分配草地,牛中的幸福度最大与幸福度 ...
- HDU 1669 二分图多重匹配+二分
Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/ ...
- POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)
解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪.每头猪对每一个猪圈有一个惬意值.要求安排这 ...
随机推荐
- 多线程编程之Apue3rd_Chapter15.10之posix信号量
看了APUE的chapter15,只重点看了15.10,学习了posix信号量.Posix信号量比起xsi信号量的优点是性能更好,在Linux3.2.0平台上性能提升很大.其中命名信号量使用方法如下. ...
- [BZOJ1455]罗马游戏(左偏树)
用并查集和左偏树维护士兵的关系 Code #include <cstdio> #include <algorithm> #define N 1000010 using name ...
- python2.7入门---变量类型&案例
这篇文章呢,主要是用来记录python中的变量类型学习内容的.接下来就来看一下变量类型,那么什么是变量呢.变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间.基于变量的数据类型,解 ...
- Spring+Hiberate 多数据源的网文整理
解决方案: http://www.th7.cn/Program/java/2011/10/23/44664.shtml 分析共享Spring配置数据源四种方式(附相应jar包) :http://ww ...
- 引用外部静态库(.a文件)时或打包.a时,Category方法无法调用。崩溃
我的这个是MJRefresh,学习打.a包Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ...
- IOException: win32 io returned 267. Path:
unity3d在导出android项目时出现了这个错误,找了一圈也没找到原因,最后把项目名中空格去掉后OK了,坑啊!!!!
- [转]struct2 拦截所有没有登录的用户,强行转到登录界面AuthorizationInterceptor
package com.sise.action; import java.util.Map; import com.opensymphony.xwork2.Action; import com ...
- jsp 路径问题和环境路径以及各种路径总结
首先确定问题: 浏览器发送请求后,服务器会返回一个响应,但是返回的网页中,会有各种路径问题,所以在此用jsp中的属性来解决.(只是记录问题,用了不专业的术语,请见谅.) 总结: 以路径 http:/ ...
- React开发时候注意点
JSX 使用jsx的使用,用一个{}包裹起来,例如 const method = {<div> 123 </div>} 使用()小括号,防止分号自动插入 const eleme ...
- Qt 解析网络数据出现ssl错误
最近写了点小东西,哈哈, 网络部分是同学帮我搞的 在编译的时候,出现了一下错误 qt.network.ssl: QSslSocket: cannot call unresolved function ...