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名学生(包含 ...
随机推荐
- Cpu实验
实验十一.基于符合ISO/IEC 7816 标准协议的CPU卡RATS.PPS请求指令操作 实验目的 1.学习和了解ISO/IEC 7816标准. 2.学习和了解ATS各字节的具体定义. 3.学习和了 ...
- 我的cocos2d-x集成sharesdk之旅(转)
链接地址:http://blog.csdn.net/yeungxuguang/article/details/18227153 本文出自:http://www.iteye.com/topic/1130 ...
- php 学习笔记 数组3
15.使用数组 1).并集(union) array_merge(array1,array2,array3..) 函数把两个或多个数组合并为一个数组,后面覆盖前面 2). 交集(intersecti ...
- java --- 对象的创建过程
java 对象创建的过程 存在了继承关系之后,对象创建过程如下: 1.分配空间.要注意的是,分配空间不光是分配子类的空间,子类对象中包含的父类对象所需要的空间,一样在这一步统一分配.在分配的空间的时候 ...
- Qt中文件操作遇到的(变量,容器,结构体)
咳咳!总结了一下我在使用QT文件操作时所用到的,再接再厉!再接再厉!! 1.保存到文件: QFile file("test.txt"); if (!file.open(QIODev ...
- Python 2.7 学习笔记 字典(map)的使用
python中的字典,就是通常说的map,即 key/value集合的数据结构. 本文来介绍下在python下如何使用字典. 对于map这种数据结构能干什么,我们就不说了,这是一个常见的数据结构,我们 ...
- About Us - Tech in Asia - Tech in Asia
About Us - Tech in Asia - Tech in Asia About us Asia is big. Its place in the world, even bigger. Te ...
- Eclipse3.6 添加JUnit源代码
Eclipse中无法查看JUnit源代码,也无法设置源代码的jar. 解决方法: 1. 下载org.junit.source_4.8.1.v4_8_1_v20100427-1100.jar,放到ec ...
- java里,当long与上了int
long switchState = 0xf0000000000L; int result = (switchState & 0xff00000000L) > 0 ? 0x01 : 0x ...
- Android中的单元测试
2015年5月19日 23:10 在Android中,已经内置了Junit所以不需要在导包.只要继承AndroidTestCase类就可以了. 首先需要修改AndroidManifes ...