解题报告

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;
}
Steady Cow Assignment
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5369   Accepted: 1845

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.

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

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.

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. POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】

     Steady Cow Assignment Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  3. POJ3189:Steady Cow Assignment(二分+二分图多重匹配)

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

  4. kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树

    二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...

  5. POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 20262   Accepted: 7230 ...

  6. POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  7. hihoCoder 1393 网络流三·二分图多重匹配(Dinic求二分图最大多重匹配)

    #1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小H ...

  8. 稳定的奶牛分配 && 二分图多重匹配+二分答案

    题意: 农夫约翰有N(1<=N<=1000)只奶牛,每只奶牛住在B(1<=B<=20)个奶牛棚中的一个.当然,奶牛棚的容量有限.有些奶牛对它现在住的奶牛棚很满意,有些就不太满意 ...

  9. hiho 第117周 二分图多重匹配,网络流解决

    描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小Ho作为班上的班干部,统计分配比赛选手的重任也自然交到了他们手上. 已知小Hi和小Ho所在的班级一共有N名学生(包含 ...

随机推荐

  1. ASP.NET事务存储过程

    --修改存储过程 alter proc proc_get_student as select * from student; asp.net 的事务就是针对数据层来处理的呀! 没有数据处理不能使用事务 ...

  2. win7 php 配置多个网站

    1.在C:\WINDOWS\system32\drivers\etc目录下,打开Hosts 添加A站和B站的DNS映射,如127.0.0.1 local.zhengxin.com127.0.0.1 l ...

  3. PyQt中如何隐藏Menu

    PyQt中隐藏一个Menu Item,可以通过QAction的setVisible(False)来设置,而QMenu的setVisible(False)是不管用的. 现在问题来了,我们有一个菜单,它有 ...

  4. 设置QPushButton的平面与突出(遍历控件)

    #include "ui_maindialog.h" #include "maindialog.h" #include <QState> #incl ...

  5. 17.1.1 How to Set Up Replication

    17.1.1 How to Set Up Replication 17.1.1.1 Setting the Replication Master Configuration 17.1.1.2 Sett ...

  6. C++常变量

    在定义变量时,如果加上关键字const,则变量的值在程序运行期间不能改变,这种变量称为常变量(constant variable).例如:    const int a=3;  //用const来声明 ...

  7. 幻世(OurDream)2D图形引擎使用教程11——播放媒体文件(1)

    声明:本教程版权归Lizcst Software Lab所有,欢迎转载,但是转载必须保留本段声明文字,并注明文章来源:http://blog.csdn.net/kflizcst 谢谢合作! 播放媒体是 ...

  8. 一天一个类,一点也不累之TreeSet

    一天一个类,一点也不累. 现在要说的是---TreeSet public class TreeSet<E> extends AbstractSet<E> implements ...

  9. android之View的启动过程

    转自:http://www.cdtarena.com/gpx/201308/9607.html 程序里调用了onSizeChanged方法进行了一些设置,不知道onSizeChanged是在什么时候启 ...

  10. javascript:设置URL参数的方法,适合多条件查询

    适用场景:多条件查询情况,如下图所示: 通过设置URL参数,再结合数据源控件设置的RUL参数,就能进行简单的多条件查询了. javascript函数: <mce:script type=&quo ...