解题报告

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. 键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试

    键盘过滤第一个例子ctrl2cap(4.1~4.4)汇总,测试 完整源代码 /// /// @file ctrl2cap.c /// @author wowocock /// @date 2009-1 ...

  2. iOS Development: Proper Use of initWithNibName:bundle: Affects UITableViewController

    Address:http://www.outofcore.com/2011/07/ios-development-proper-use-of-initwithnibnamebundle-affects ...

  3. codeigniter ,看完这些,就可以用它做项目了

    一.MVC 1,入口文件 唯一一个让浏览器直接请求的脚本文件 2,控制器 controller 负责协调模型和视图 3,模型 model 只负责提供数据,保存数据 4,视图 只负责显示,以及搜集用户的 ...

  4. MySQL教程及经常使用命令1.1

    在线教程 21分钟 MySQL 新手教程 w3school在线教程(MYSQL) 变量 查看系统变量 show global variables 查看详细变量 show global variable ...

  5. UVA 10763 Foreign Exchange 出国交换 pair+map

    题意:给出很多对数字,看看每一对(a,b)能不能找到对应的(b,a). 放在贪心这其实有点像检索. 用stl做,map+pair. 记录每一对出现的次数,然后遍历看看对应的那一对出现的次数有没有和自己 ...

  6. [LeetCode] Decode Ways [33]

    题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...

  7. 安装Devstack的DNS问题

    所谓的OpenStack一键安装,省去了敲键盘的麻烦,但是卡在中间出了问题也是比较尴尬的 在公司内安装经常会出现卡在下载软件的地方,有时候还会出错 trick就是换一个US的dns,比如8.8.8.8

  8. Android学习笔记之View(二)

    View加载的流程之测量:rootView调用measure()→onMeasure(): measure()是final方法,表明Android不想让开发者去修改measure的框架,开发者可以on ...

  9. java io学习记录(路径分隔符)

    java路径分隔符(路径表示) path="E:\\xp\\test\\2.jpg"; path="E:/xp/test/2.jpg"; path=" ...

  10. JVM调优总结(九)-新一代的垃圾回收算法

    垃圾回收的瓶颈 传统分代垃圾回收方式,已经在一定程度上把垃圾回收给应用带来的负担降到了最小,把应用的吞吐量推到了一个极限.但是他无法解决的一个问题,就是Full GC所带来的应用暂停.在一些对实时性要 ...