解题报告

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. Linq实现t-Sql的各种连接

    在ORM框架大行其道的今天,对于.net行业的人,想要学好EF,那Linq的学习在势在必行啊.今天总结下平时比较常用的表连接的用法. Inner Join Linq: var list = (from ...

  2. B - 畅通工程(并查集)

    对并查集理解之后就可以做这种题了,虽说这种题做的不多,这道题做过才这么快搞定,可是还是挺happy滴,加油 Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接 ...

  3. A - Oil Deposits(搜索)

    搜索都不熟练,所以把以前写的一道搜索复习下,然后下一步整理搜索和图论和不互质的中国剩余定理的题 Description GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp ...

  4. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  5. IPython在Windows 7上的搭建步骤

    安装篇 pip install jupyter 使用篇 启动本地notebook,打开Windows命令行,键入:ipython notebook

  6. django ImageField用法

    settings里的设置 PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname('__file__'))) MEDIA_ROOT = ...

  7. 原生js仿jquery--animate效果

    效果 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  8. 基于visual Studio2013解决C语言竞赛题之0509杨辉三角

     题目

  9. Struts2 学习笔记16 struts标签 part2

    接下来说一下if标签.下面是结果图. <li><s:if test="#parameters.age[0]<0">error!</s:if> ...

  10. Linux高性能server编程——Linux网络基础API及应用

     Linux网络编程基础API 具体介绍了socket地址意义极其API,在介绍数据读写API部分引入一个有关带外数据发送和接收的程序,最后还介绍了其它一些辅助API. socket地址API 主 ...