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 ;
}

最新文章

  1. iOS多图片下载
  2. mybatis原理
  3. 【Unity3D基础教程】给初学者看的Unity教程(一):GameObject,Compoent,Time,Input,Physics
  4. IO流--切割 合并文件
  5. 【转】shell脚本调试(bash trap support bashdb )
  6. android的个人代码总结
  7. /lib /usr/lib /usr/local/lib 区别
  8. new 与override 区别
  9. 代码中动态改变布局属性RelativeLayout.LayoutParams.addRule()
  10. border-image详解
  11. 让VS2019支持.NET Core WinForms和WPF设计器的临时办法(比微软给出的办法更方便)
  12. Nodejs nmp 常用命令
  13. [转]tableExport.js 导出excel 如果有负数或是空值 导出前面会自动加上单引号
  14. NYOJ_1274_信道安全 -
  15. 04-接口隔离原则(ISP)
  16. @JsonInclude注解,RestTemplate传输值为null的属性,利用FastJson将属性中有空值null的对象转化成Json字符串
  17. 常用的服务发现对比(Consul、zookeeper、etcd、eureka)
  18. Kibana访问权限控制
  19. python中的多进程
  20. python安装 numpy&amp;安装matplotlib&amp; scipy

热门文章

  1. 07 json与os模块(进阶)
  2. CDSビュー新規作成
  3. myeclipse 安装pydev插件后svn插件失效
  4. Python爬虫之JS异步加载
  5. 20145202马超 实验二《Java面向对象程序设计》实验报告
  6. Error loading MySQLdb module: libmysqlclient.so.18: cannot open shared object file: No such file or directory
  7. 高德API+.NET解决租房问题(JS相关)
  8. iOS笔记054 - 核心动画
  9. PL/SQL查看表结构
  10. python学习总结----内置函数及数据持久化