Steady Cow Assignment POJ - 3189 (最大流+匹配)
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.
枚举牛棚的最差排名和最好排名(即枚举排名差) ,在这个排名之内牛和这个牛棚建边。
看看每种情况判断是否合法(是否满流),取最小值。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
int d[maxn], head[maxn], in[maxn], cur[maxn], w[][], abi[maxn];
int n, m, s, t, minn;
int cnt = ; struct node
{
int u, v, c, next;
}Node[*maxn]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(!d[e.v] && e.c > )
{
d[e.v] = d[e.u] + ;
Q.push(e.v);
if(e.v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = , V;
if(u == t || cap == )
return cap;
for(int &i=cur[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] == d[u] + && e.c > )
{
int V = dfs(e.v, min(cap, e.c));
Node[i].c -= V;
Node[i^].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int dinic(int u)
{
int ans = ;
while(bfs())
{
memcpy(cur, head, sizeof(head));
ans += dfs(u, INF);
}
return ans;
} int main()
{
scanf("%d%d", &n, &m);
minn = INF;
s = , t = n + m + ;
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
{
scanf("%d",&w[i][j]);
}
for(int i=; i<=m; i++)
scanf("%d",&abi[i]);
for(int h=; h<=m; h++)
{
for(int i=h; i<=m; i++)
{
mem(head, -);
cnt = ;
for(int k=; k<=n; k++)
add(s, k, );
for(int k=; k<=m; k++)
add(n+k, t, abi[k]);
for(int j=; j<=n; j++)
{
for(int k=h; k<=i; k++)
add(j, n+w[j][k], );
}
if(dinic(s) == n)
{
minn = min(minn, i-h+);
}
}
}
printf("%d\n",minn); return ;
}
Steady Cow Assignment POJ - 3189 (最大流+匹配)的更多相关文章
- O - Steady Cow Assignment - POJ 3189(多重匹配+枚举)
题意:有N头奶牛,M个牛棚,每个牛棚都有一个容量,并且每个牛对牛棚都有一个好感度,现在重新分配牛棚,并且使好感觉最大的和最小的差值最小. 分析:好感度貌似不多,看起来可以枚举一下的样子,先试一下把 注 ...
- POJ3189:Steady Cow Assignment(二分+二分图多重匹配)
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7482 Accepted: ...
- POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
Steady Cow Assignment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- Poj 3189 Steady Cow Assignment (多重匹配)
题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...
- POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65 ...
- POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment
这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...
- POJ3189 Steady Cow Assignment
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6817 Accepted: ...
- POJ 3189 Steady Cow Assignment 【二分】+【多重匹配】
<题目链接> 题目大意: 有n头牛,m个牛棚,每个牛棚都有一定的容量(就是最多能装多少只牛),然后每只牛对每个牛棚的喜好度不同(就是所有牛圈在每个牛心中都有一个排名),然后要求所有的牛都进 ...
- POJ 3189 Steady Cow Assignment
题意:每个奶牛对所有的牛棚有个排名(根据喜欢程度排的),每个牛棚能够入住的牛的数量有个上限,重新给牛分配牛棚,使牛棚在牛心中的排名差(所有牛中最大排名和最小排名之差)最小. 题目输入: 首先是两个 ...
随机推荐
- 【转】软件质量之道:SourceMonitor
转:https://blog.csdn.net/feng_ma_niu/article/details/40594799 SourceMonitor是一个源代码衡量工具,由http://www.cam ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
- kubernetes 的wen pod 无法连接 mysql 的pod
1.分析 查看源代码 既然无法建立连接,那先看下是如何建立连接的.登录到myweb的docker容器里面,查看index.jsp文件,主要内容如下: Class.forName("com.m ...
- Json.NET序列化后包含类型,保证序列化和反序列化的对象类型相同(转载)
This sample uses the TypeNameHandlingsetting to include type information when serializing JSON and r ...
- 番外篇:因为一个固态导致的——系统重装与JAVA软件环境下载安装配置
第一步:拆机改装 最近想换个固态硬盘提提速度,就买了个,然后拧下后盖螺丝,将键盘盖拿下,中间有两个根线连着把扣打开就可以了,将新的固态硬盘装到原本的机械硬盘的地方,又买了个光驱托盘改装位将光驱位装上了 ...
- 不成功的RMAN恢复到其他机器的例子
事实上,RMAN备份的时候,如果是使用control file 来作catalog,那么一定要把control file和spfile恢复到另外的机器上面. 否则,会出现类似如下的错误: 原来的实例: ...
- Scala学习(一)--Scala基础学习
Scala基础学习 摘要: 在篇主要内容:如何把Scala当做工业级的便携计算器使用,如何用Scala处理数字以及其他算术操作.在这个过程中,我们将介绍一系列重要的Scala概念和惯用法.同时你还将学 ...
- Dynamics CRM Online Administrator password reset
道道还挺多,好好看看 Dynamics CRM Online Administrator password reset
- Unity利用SMSSDK实现短信验证码(附代码)
最近一直在研究如何给app更多实用性的功能,在app进行登录或者注册时,为了方便用户更加快捷的完成登录功能,所以就决定采用短信验证码的方式进行验证登录.在学习的过程中,先使用了Mob的短信服务进行短信 ...
- cocos2d-x学习之路(三)——精灵与动作
这里我们来看看所有游戏引擎中都会出现的一个重要的概念——精灵