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.

 
用最大流求多重匹配  https://www.cnblogs.com/WTSRUVF/p/9312104.html

枚举牛棚的最差排名和最好排名(即枚举排名差) ,在这个排名之内牛和这个牛棚建边。

看看每种情况判断是否合法(是否满流),取最小值。

#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 (最大流+匹配)的更多相关文章

  1. O - Steady Cow Assignment - POJ 3189(多重匹配+枚举)

    题意:有N头奶牛,M个牛棚,每个牛棚都有一个容量,并且每个牛对牛棚都有一个好感度,现在重新分配牛棚,并且使好感觉最大的和最小的差值最小. 分析:好感度貌似不多,看起来可以枚举一下的样子,先试一下把 注 ...

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

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

  3. POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】

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

  4. Poj 3189 Steady Cow Assignment (多重匹配)

    题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...

  5. POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65 ...

  6. POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment

    这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...

  7. POJ3189 Steady Cow Assignment

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

  8. POJ 3189 Steady Cow Assignment 【二分】+【多重匹配】

    <题目链接> 题目大意: 有n头牛,m个牛棚,每个牛棚都有一定的容量(就是最多能装多少只牛),然后每只牛对每个牛棚的喜好度不同(就是所有牛圈在每个牛心中都有一个排名),然后要求所有的牛都进 ...

  9. POJ 3189 Steady Cow Assignment

    题意:每个奶牛对所有的牛棚有个排名(根据喜欢程度排的),每个牛棚能够入住的牛的数量有个上限,重新给牛分配牛棚,使牛棚在牛心中的排名差(所有牛中最大排名和最小排名之差)最小.   题目输入: 首先是两个 ...

随机推荐

  1. 如何高效的通过BP算法来训练CNN

    < Neural Networks Tricks of the Trade.2nd>这本书是收录了1998-2012年在NN上面的一些技巧.原理.算法性文章,对于初学者或者是正在学习NN的 ...

  2. Eclipse-设置启动JDK版本

    打开eclipse安装目录下的eclipse.ini文件,将红色内容加入 -vm ../Java/jdk1.6.0_26/bin (或者指向具体目录:D:/software/jdk_1.8u91/bi ...

  3. 20155238 《JAVA程序设计》实验三(敏捷开发与XP实践)实验报告

    实验内容 敏捷开发与XP实践 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2.完成实 ...

  4. C++之enum枚举量声明、定义、使用与枚举类详解

    C++之enum枚举量声明.定义.使用与枚举类详解 学习一个东西,首先应该指导它能做什么,其次去知道它怎么去做,最后知道为什么去这么做. 知其然知其所以然.不能冒进 ,一步一步的慢慢来.

  5. Jmeter 安装后无法启动问题

    问题:按照教程java环境安装完成,也下载了 Jmeter  安装包,但是在启动的时候 dos窗口就一直提示下面的错误信息 ‘findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件.  ...

  6. WPF的DataTrigger绑定自身属性

    原文:WPF的DataTrigger绑定自身属性 <DataTrigger Binding="{Binding RelativeSource={RelativeSource self} ...

  7. 【第四课】Linux的基础命令使用

    目录 一.passwd重置密码 二.单用户模式 三.救援模式 四.设置SElinux 五.Linux的常用基础命令详解 5.1.mkdir命令 5.2.ls命令 5.3.cd命令 5.4.chmod命 ...

  8. 微服务监控zipkin、skywalking以及日志ELK监控系列

    0.整体架构 整体架构目录:ASP.NET Core分布式项目实战-目录 一.目录 1.zipkin监控 2.skywalking监控 3.ELK日志监控 asp.net Core 交流群:78746 ...

  9. 新的旅程:NodeJS - 环境篇

    用ASP.NET MVC好多年了,还记得当初为MVC所倡导的"DRY"理念所感染,为Razor的简单而震撼.随着MVC的成熟反而让我觉得似乎渐渐地走入了微软营造的一种高技术的牢笼. ...

  10. JS基础内容小结(DOM&&BOM)(二)

    元素.childNodes:只读 属性 子节点列表集合 元素.nodeType:只读 属性 当前元素下的节点类型 元素.attributes : 只读 属性 属性列表集合 元素.children: 只 ...