POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)
解题报告
http://blog.csdn.net/juncoder/article/details/38340447
题意:
B个猪圈,N头猪。每头猪对每一个猪圈有一个惬意值。要求安排这些猪使得最大惬意和最小惬意的猪差值最小
思路:
二分图的多重匹配问题;
猪圈和源点连边,容量为猪圈容量。猪与汇点连边,容量1;
猪圈和猪之间连线取决所取的惬意值范围;
二分查找惬意值最小差值的范围。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 99999999
using namespace std;
int n,m,b,mmap[1030][22],edge[1030][1030],l[1030],c[22]; int bfs()
{
memset(l,-1,sizeof(l));
l[0]=0;
int i;
queue<int >Q;
Q.push(0);
while(!Q.empty()) {
int u=Q.front();
Q.pop();
for(i=0; i<=m; i++) {
if(edge[u][i]&&l[i]==-1) {
l[i]=l[u]+1;
Q.push(i);
}
}
}
if(l[m]>1)return 1;
return 0;
}
int dfs(int x,int f)
{
if(x==m)return f;
int i,a;
for(i=0; i<=m; i++) {
if(l[i]==l[x]+1&&edge[x][i]&&(a=dfs(i,min(f,edge[x][i])))) {
edge[x][i]-=a;
edge[i][x]+=a;
return a;
}
}
l[x]=-1;
return 0;
}
int dinic()
{
int ans=0,a;
while(bfs())
while(a=dfs(0,inf))
ans+=a;
return ans;
}
int cow(int mid)
{
int i,j,k;
for(i=1; i<=b-mid+1; i++) {
memset(edge,0,sizeof(edge));
for(j=1; j<=b; j++) {
edge[0][j]=c[j];
}
for(j=1; j<=n; j++) {
for(k=i; k<=i+mid-1; k++) {
edge[mmap[j][k]][j+b]=1;
}
edge[j+b][m]=1;
}
if(dinic()==n)
return 1;
}
return 0;
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&b)) {
memset(mmap,0,sizeof(mmap));
memset(c,0,sizeof(c));
m=n+b+1;
for(i=1; i<=n; i++) {
for(j=1; j<=b; j++) {
scanf("%d",&mmap[i][j]);
}
}
for(i=1; i<=b; i++) {
scanf("%d",&c[i]);
}
int l=1,r=b,t=-1;
while(l<=r) {
int mid=(l+r)/2;
if(cow(mid)) {
t=mid;
r=mid-1;
} else {
l=mid+1;
}
}
printf("%d\n",t);
}
return 0;
}
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 5369 | Accepted: 1845 |
Description
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
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
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
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.
POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)的更多相关文章
- POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65 ...
- POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
Steady Cow Assignment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ3189:Steady Cow Assignment(二分+二分图多重匹配)
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7482 Accepted: ...
- kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树
二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...
- POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 20262 Accepted: 7230 ...
- POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS Memory Limit: 30000K T ...
- hihoCoder 1393 网络流三·二分图多重匹配(Dinic求二分图最大多重匹配)
#1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小H ...
- 稳定的奶牛分配 && 二分图多重匹配+二分答案
题意: 农夫约翰有N(1<=N<=1000)只奶牛,每只奶牛住在B(1<=B<=20)个奶牛棚中的一个.当然,奶牛棚的容量有限.有些奶牛对它现在住的奶牛棚很满意,有些就不太满意 ...
- hiho 第117周 二分图多重匹配,网络流解决
描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小Ho作为班上的班干部,统计分配比赛选手的重任也自然交到了他们手上. 已知小Hi和小Ho所在的班级一共有N名学生(包含 ...
随机推荐
- CentOS 6 用SVN自动提交文件到web服务器
关于 svn 的安装 参考:[转]Linux(centOS6.5)下SVN的安装.配置及开机启动 经过两天的各种尝试总算解决了,总结如下: 1.在建立库时注意 要让库的名称和 要同步的 web目录名 ...
- __autoload函数
./index.php----------------------------------------------------------------------------------<?ph ...
- Codeforces 509C Sums of Digits 贪心
这道题目有人用DFS.有人用DP 我觉得还是最简单的贪心解决也是不错的选择. Ok,不废话了,这道题目的意思就是 原先存在一个严格递增的Arrary_A,然后Array_A[i] 的每位之和为Arra ...
- 使用Android studio下载github上的工程及问题解决
Android studio内置了github的插件,可以直接下载github上的工程,感觉好爽啊.具体怎么做呢?1.如图所示操作,如果是初次使用会提示输入用户名密码. 2.等android stud ...
- OGR SQL
The OGRDataSource supports executing commands against a datasource via the OGRDataSource::ExecuteSQL ...
- Java 找出四位数的全部吸血鬼数字 基础代码实例
/** * 找出四位数的全部吸血鬼数字 * 吸血鬼数字是指位数为偶数的数字,能够由一对数字相乘而得到,而这对数字各包括乘积的一半位数的数字,当中从最初的数字中选取的数字能够随意排序. * 以两个 ...
- 前序 中序 后序 遍历 递归 非递归算法 java实现
前序遍历 非递归 public void preordernorec(TreeNode root){ //System.out.println("先序遍历(非递归):"); //用 ...
- 第1章 Lua基础
1.1 全局变量 全局变量不需要声明,给一个变量赋值后即创建了这个全局变量,访问一个没有初始化的全局变量也不会出错,只不过得到的结果是:nil. 如果你想删除一个全局变量,只需要将变量负值为 nil ...
- 在Vista以上版本运行WTL程序,有时候会提示“这个程序可能安装补正确...”的错误
在Win7/Vista下,如何以兼容模式运行exe? https://msdn.microsoft.com/en-us/library/dd371711(VS.85).aspx 问题描 ...
- 第一个hibernate文件 xml配置方法
package com.entity; public class User { private String username; private String password; private In ...