枚举 转化为可行性判定问题 网络流 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 个点的无向图,求至少删去多少个点可以使得无向图不连通. 题解:学习到了点边转化思想. 根据网络流的知识可知,一个网络的最小割与网络的最大流相等.不过最小割是图的边集,而本题则是 ...
随机推荐
- oracle获取表字段及表注释的相关操作
一.获取表字段: select * from user_tab_columns where Table_Name='用户表' user_tab_columns 为当前用户的columns,除此之外还有 ...
- 【Linux常见问题】CentOS 7 root用户密码忘记,找回密码方法
1.开机按esc 2.选择CentOS Linux (3.10.0-693.......) 按 e 键: 3.光标移动到 linux 16 开头的行,找到 ro 改为 rw init=sysr ...
- MutationObserver 监听 DOM 树变化
MutationObserver 是用于代替 MutationEvents 作为观察 DOM 树结构发生变化时,做出相应处理的 API .为什么要使用 MutationObserver 去代替 Mut ...
- 深度解析国内首个云原生数据库POLARDB的“王者荣耀”
随着移动互联网.电子商务的高速发展,被使用最多的企业级开源数据系统MySQL面临着巨大挑战--为迎接"双11"的高并发要提前做好分库分表;用户不断激增要将读写分离才能应对每天上亿次 ...
- Docker PHP7官方镜像安装Redies扩展
2019独角兽企业重金招聘Python工程师标准>>> 直接RUN docker-php-ext-install redis 失败,google得到: ENV PHPREDIS_VE ...
- 2019.06.18训练日记(赞FLS)
之前打了几场比赛,有很多题没做出来,这些题无论是知识点不会,还是说在当时时间和思路的影响下没有做出来,这都应该做出来,至少现在必须做出来,本来打算专心复习,分数高了,好保研,但是想了想如果局限于只把学 ...
- AWVS 安全渗透扫描
1.打开软件,点击 New Scan 2.在 website url 中输入被扫描的网址,点击 next 3.在 scanning profile 中选择测试的漏洞类型,默认选择 default(默认 ...
- 王颖奇 201771010129《面向对象程序设计(java)》第八周学习总结
实验六 接口的定义与使用 实验时间 2018-10-18 1.实验目的与要求 (1) 掌握接口定义方法: (2) 掌握实现接口类的定义要求: (3) 掌握实现了接口类的使用要求: (4) 掌握程序回调 ...
- Bootstrap Table 3 官方文档
备查 Bootstrap Table 3 官方文档 示例
- 带你看看Java的锁(三)-CountDownLatch和CyclicBarrier
带你看看Java中的锁CountDownLatch和CyclicBarrier 前言 基本介绍 使用和区别 核心源码分析 总结 前言 Java JUC包中的文章已经写了好几篇了,首先我花了5篇文章从源 ...