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头猪.每头猪对每一个猪圈有一个惬意值.要求安排这 ...
随机推荐
- jmeter测试报告优化
1.下载jmeter.results.shanhe.me.xsl 将该文件拷贝到jmeter\extras目录下 2.修改jmeter.results.shanhe.me.xsl 这里直接拷贝 jme ...
- maven 添加自己下载的jar包到本地仓库
1.在pom文件中添加依赖,其中groupId等变量都自拟. 例如: 2.在命令行执行以下命令,提示build success即表示安装成功. mvn install:install-file -Dg ...
- (数据科学学习手札08)系统聚类法的Python源码实现(与Python,R自带方法进行比较)
聚类分析是数据挖掘方法中应用非常广泛的一项,而聚类分析根据其大体方法的不同又分为系统聚类和快速聚类,其中系统聚类的优点是可以很直观的得到聚类数不同时具体类中包括了哪些样本,而Python和R中都有直接 ...
- 获取单片机唯一id(stm32获取单片机唯一id)
stm32唯一id: 不同型号的stm32单片机,id不在同一地址上!具体地址可以通过用户手册中的Device electronic signature>Unique device ID reg ...
- 【jQuery】 效果
[jQuery] 效果 资料 http://www.w3school.com.cn/jquery/jquery_ref_effects.asp 1. 显示隐藏 hide(); 隐藏 show(): 显 ...
- 「暑期训练」「基础DP」 Monkey and Banana (HDU-1069)
题意与分析 给定立方体(个数不限),求最多能堆叠(堆叠要求上方的方块严格小于下方方块)的高度. 表面上个数不限,问题是堆叠的要求决定了每个方块最多可以使用三次.然后就是对3n" role=& ...
- Qt 使用irrlicht(鬼火)3D引擎
项目中需要加载简单的3D场景.资深老前辈推荐使用开源小巧的引擎irrlicht. 关于irrlicht,来之百度百科 Irrlicht引擎是一个用C++书写的高性能实时的3D引擎,可以应用于C++程序 ...
- Spark集群管理器介绍
Spark可以运行在各种集群管理器上,并通过集群管理器访问集群中的其他机器.Spark主要有三种集群管理器,如果只是想让spark运行起来,可以采用spark自带的独立集群管理器,采用独立部署的模式: ...
- QR码与DM码的区别
DM无法表现汉字等其他形式,而QR码能用数据压缩方式来表示汉字,仅用13bit即可表示一个汉字,比其他二维条码表示汉字的效率提高了20%.相较而言,DM码信息容量小,应用简单.而QR在汉字处理上更有优 ...
- 阿里云服务器安装https证书 centos + httpd + Symantec
一. 环境 centos7 阿里云服务器, httpd服务, 阿里云免费的Symantec证书 阿里云Symantec 有个免费版的证书, 具体怎么申请可以去百度解决 二. 网上大部分的经验贴都是要A ...