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(二分+二分图多重匹配)的更多相关文章

  1. POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65 ...

  2. POJ3189 Steady Cow Assignment

    Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6817   Accepted:  ...

  3. Poj 2289 Jamie's Contact Groups (二分+二分图多重匹配)

    题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多 ...

  4. [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)

    http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...

  5. POJ2289:Jamie's Contact Groups(二分+二分图多重匹配)

    Jamie's Contact Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/ ...

  6. POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment

    这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...

  7. POJ3189 Steady Cow Assignment(最大流)

    题目大概说,有n头牛和b块草地,每头牛心中分别对每块草地都有排名,草地在牛中排名越高牛安排在那的幸福度就越小(...),每块草地都能容纳一定数量的牛.现在要给这n头牛分配草地,牛中的幸福度最大与幸福度 ...

  8. HDU 1669 二分图多重匹配+二分

    Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  9. POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)

    解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪.每头猪对每一个猪圈有一个惬意值.要求安排这 ...

随机推荐

  1. [BZOJ1076][SCOI2008]奖励关(概率DP)

    Code #include <cstdio> #include <algorithm> #include <cstring> #define N 110 #defi ...

  2. JavaScript---设计模式之迭代器模式

    迭代器模式提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该方法中的内部表示. jQuery中我们经常会用到一个each函数就是迭代器模式 作用 为遍历不同的集合结构提供一个统一的接口,从而 ...

  3. MySQL server has gone away 错误处理

    解决方案1: 这个是mysql自身的一个机制:     mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决办法有两个:     1.修改mysql 配置               增 ...

  4. 可用率map处理

    total_data =[ {'event_current_dealer': '陈铁', 'id__count': 66}, {'event_current_dealer': '丁凯', 'id__c ...

  5. hibernate 各历史版本下载 spring各历史版本下载

    hibernate 各历史版本下载http://sourceforge.net/projects/hibernate/files/ spring各历史版本下载http://www.springsour ...

  6. Bash中使用MySQL导入导出CSV格式数据[转]

    转自: http://codingstandards.iteye.com/blog/604541 MySQL中导出CSV格式数据的SQL语句样本如下:   select * from test_inf ...

  7. gitk中文乱码问题处理

    执行了 git config --global gui.encoding utf- 查看 %USERPROFILE%\.gitconfig 文件中也有 [gui] encoding = utf-8 在 ...

  8. 通过repcached实现memcached主从复制

    一.环境 服务器A:ubuntu server 12.04(192.168.1.111) 服务器B:ubuntu server 12.04 (47.50.13.111) 二.memcached安装 s ...

  9. Selenium页面工厂+数据驱动测试框架

    工程的目录结构: pom.xml文件: <?xml version="1.0" encoding="UTF-8"?><project xmln ...

  10. 跳出for循环break和continue的区别

    1.break 跳出for循环,结束for循环 如果有两层循环,break只能跳出一层循环 2.continue 跳出本次循环,继续下一条数据的循环