枚举 转化为可行性判定问题 网络流 poj3189
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6914 | Accepted: 2387 |
Description
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 二分长度+枚举
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1500;
int head[N],tot,S,T;
int q[N],dis[N],n,m;
int mp[N][22],cap[22];
struct node
{
int next,v,w;
} e[N*100];
void add(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
bool bfs()
{
memset(dis,-1,sizeof(dis));
dis[S]=0;
int l=0,r=0;
q[r++]=S;
while(l<r)
{
int u=q[l++];
for(int i=head[u]; ~i; i=e[i].next)
{
int v=e[i].v;
if(dis[v]==-1&&e[i].w>0)
{
q[r++]=v;
dis[v]=dis[u]+1;
if(v==T) return true;
}
}
}
return false;
}
int dfs(int s,int low)
{
if(s==T||!low) return low;
int ans=low,a;
for(int i=head[s]; ~i; i=e[i].next)
{
if(e[i].w>0&&dis[e[i].v]==dis[s]+1&&(a=dfs(e[i].v,min(e[i].w,ans))))
{
e[i].w-=a;
e[i^1].w+=a;
ans-=a;
if(!ans) return low;
}
}
if(low==ans) dis[s]=-1;
return low-ans;
}
bool solve(int l,int r)
{
memset(head,-1,sizeof(head));
tot=0;
int ans=0;
for(int i=1; i<=n; ++i) add(S,i,1),add(i,S,0);
for(int i=1; i<=n; ++i) for(int j=l; j<=r; ++j) add(i,mp[i][j]+n,1),add(mp[i][j]+n,i,0);
for(int i=1; i<=m; ++i) add(i+n,T,cap[i]),add(T,i+n,0);
while(bfs()) ans+=dfs(S,1008611);
if(ans==n) return true;
return false;
}
int main()
{
scanf("%d%d",&n,&m);S=0,T=n+m+1;
for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) scanf("%d",&mp[i][j]);
for(int i=1; i<=m; ++i) scanf("%d",&cap[i]);
int l=1,r=m,ans=m;
while(l<=r) {
int mid=(l+r)>>1;
bool ok=0;
for(int i=1;i+mid-1<=m;++i) if(solve(i,i+mid-1)) {
ok=1;
r=mid-1;
ans=mid;
}
if(!ok) l=mid+1;
}
printf("%d\n",ans);
}
枚举 转化为可行性判定问题 网络流 poj3189的更多相关文章
- poj2699 转化为可行性判定问题+二分枚举+最大流
The Maximum Number of Strong Kings Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2302 ...
- C#枚举转化示例大全,数字或字符串转枚举
本文重点举例说明C#枚举的用法,数字转化为枚举.枚举转化为数字及其枚举数值的判断,以下是具体的示例: 先举两个简单的例子,然后再详细的举例说明: 字符串转换成枚举:DayOfWeek week=(Da ...
- 【算法】【网络流24题】巨坑待填(成功TJ,有时间再填)
------------------------------------------------------------------------------------ 17/24 --------- ...
- OC开发中运用到的枚举
一 常见枚举的定义: typedef enum { LOGIN_SUCCESS, USER_NAME, USER_PASSWORD, OLD_LAT, OLD_LNG }FIELD_SAVED; ...
- MVC 枚举绑定 DropDownList
/// <summary> /// 枚举转化下拉列表数据集 /// </summary> /// <param name="type">类型&l ...
- codevs2189数字三角形w——最优性转化
题目:http://codevs.cn/problem/2189/ 通过增加一维,将最优性转化为可行性. 代码如下: #include<iostream> #include<cstd ...
- 将枚举转成SelectListItem
代码如下: /// <summary> /// 将一个枚举转化成一个List<SelectListItem> /// </summary> /// <type ...
- spring boot 枚举使用的坑3
上一篇说到spring boot 使用jackson在枚举enum序列化和反序列化的问题, 再来说说在JPA中实体entity使用枚举的问题. 还是这个枚举: @Getter @AllArgsCons ...
- 【UVA1660】Cable TV Network
题目大意:给定一个 N 个点的无向图,求至少删去多少个点可以使得无向图不连通. 题解:学习到了点边转化思想. 根据网络流的知识可知,一个网络的最小割与网络的最大流相等.不过最小割是图的边集,而本题则是 ...
随机推荐
- Iterator to list的三种方法
目录 简介 使用while 使用ForEachRemaining 使用stream 总结 Iterator to list的三种方法 简介 集合的变量少不了使用Iterator,从集合Iterator ...
- 《JavaScript和jQuery实战手册(原书第2版)》——2.1节语句
2.1 语句JavaScript语句是基本的编程单元,通常表示JavaScript程序中的单个步骤.可以把一条语句看做一个句子一样,就好像成串的句子一起组成一个段落(或一章,或一本书)一样,把语句组合 ...
- 算法竞赛进阶指南--在单调递增序列a中查找小于等于x的数中最大的一个(即x或x的前驱)
在单调递增序列a中查找<=x的数中最大的一个(即x或x的前驱) while (l < r) { int mid = (l + r + 1) / 2; if (a[mid] <= x) ...
- 初学dp心得
从STL到贪心,再到现在的动态规划,可以说动态规划真的让我学的有点蒙,对于一些题目,会做,但是不会用DP,现在还不能熟练的写出状态转移方程,更重要的是,自己宛如一个哺乳期的小孩,做题需要套模板,没有模 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 XKC's basketball team
XKC , the captain of the basketball team , is directing a train of nn team members. He makes all mem ...
- badboy 录制脚本
第一步:介绍badboy工具 1.1: 页面功能分析: 1. 界面视图,模拟浏览器,能够进行操作 2. 需要录制脚本的URL 3. 点击运行URL 4. Summary:运行的各指标,响应时间,成功事 ...
- Linux文件删除空间未释放
当系统空间使用量过大需要清理空间或者清理某个文件时,有时会出现执行了删除命令之后磁盘空间并没有释放,很多人首次遇到该情况时会比较困惑,在考虑是不是像windows系统的回收站一样,删除只是逻辑删除到回 ...
- 题目分享M
题意: 其实就是找到AB+min(AC,BC)的最大值 分析:刚看到这道题,很容易往AB为树的直径方向上去想,C就可以遍历每个点,最后求出对于每个点为C时的最大值 那AB到底是不是树的直径或者为什么A ...
- qt creator源码全方面分析(4-2)
目录 global头文件 global.h xx.h global头文件 插件的本质就是动态链接库,对于库,需要导出符号,供用户导入使用.在qt creator的源码中,存在固定的导入导出模式. gl ...
- Spring中资源的加载原来是这么一回事啊!
1. 简介 在JDK中 java.net.URL 适用于加载资源的类,但是 URL 的实现类都是访问网络资源的,并没有可以从类路径或者相对路径获取文件及 ServletContext , 虽然可以通过 ...