Codeforces Gym 100733I The Cool Monkeys 拆点+最大流
原题链接:http://codeforces.com/gym/100733/problem/I
题意
有两颗树(只是树,不是数据结构),每棵树上有不同高度的树枝,然后有m只猴子在某棵树的前m高的树枝上睡觉。早上的时候,他们要到地上,但他们只能从一棵树上跳到另一棵树上,并且跳的高度差也有限制,每个树枝仅能被一只猴子踩过。问你,猴子们是否能够跳到另一棵树最低的m个树枝上。
题解
就每个点拆成两个点,然后连边,所有边的容量都为1,然后跑发最大流,考察是否为滿流即可。
代码
#include<iostream>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<queue>
#define MAX_S (1<<10)+10
#define MAX_V 2222
#define MAX_N MAX_V
#define INF 1000000009
using namespace std; struct edge {
int to, cap, rev;
bool isRev; edge(int t, int c, int r, bool i)
: to(t), cap(c), rev(r), isRev(i) { } edge() { }
}; template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF) return ; //EOF
while(c!=' -' &&(c<'' ||c>'' )) c=getchar();
sgn=(c==' -' )?-:;
ret=(c==' -' )?:(c-'' );
while(c=getchar(),c>='' &&c<='' ) ret=ret*+(c-'' );
ret*=sgn;
return ;
} vector<edge> G[MAX_N];
int level[MAX_V];
int iter[MAX_V]; void init(int totNode) {
for (int i = ; i <= totNode; i++)
G[i].clear();
memset(level, , sizeof(level));
memset(iter, , sizeof(iter));
} void add_edge(int from,int to,int cap) {
G[from].push_back(edge (to, cap, G[to].size(),));
G[to].push_back(edge (from, , G[from].size() - ,));
} void bfs(int s) {
queue<int> que;
memset(level, -, sizeof(level));
level[s] = ;
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (int i = ; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > && level[e.to] < ) {
level[e.to] = level[v] + ;
que.push(e.to);
}
}
}
} int dfs(int v,int t,int f) {
if (v == t)return f;
for (int &i = iter[v]; i < G[v].size(); i++) {
edge &e = G[v][i];
if (e.cap > && level[v] < level[e.to]) {
int d = dfs(e.to, t, min(f, e.cap));
if (d > ) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return ;
} int max_flow(int s,int t) {
int flow = ;
for (; ;) {
bfs(s);
if (level[t] < )return flow;
memset(iter, , sizeof(iter));
int f;
while ((f = dfs(s, t, INF)) > ) {
flow += f;
}
}
} int ha[MAX_N],hb[MAX_N];
int m,na,nb,t; int S=;
int T=; bool cmp(int a,int b){
return a>b;
} int main() {
cin.sync_with_stdio(false);
cin >> m >> na >> nb >> t;
for (int i = ; i < na; i++)cin >> ha[i];
for (int i = ; i < nb; i++)cin >> hb[i];
sort(ha, ha + na,cmp);
sort(hb, hb + nb,cmp);
for(int i=;i<m;i++)
add_edge(S,i,);
for(int i=;i<na;i++)
add_edge(i,i+na,);
for(int i=;i<=m;i++)
add_edge((nb-i)+na+na+nb,T,);
for(int i=;i<nb;i++)
add_edge(i+na+na,i+na+na+nb,);
for(int i=;i<na;i++)
for(int j=;j<nb;j++)
if(abs(ha[i]-hb[j])<t){
add_edge(i+na,j+*na,);
add_edge(j+*na+nb,i,);
}
int f=max_flow(S,T);
if(f>=m){
cout<<"S"<<endl;
return ;
}
init(MAX_N);
for(int i=;i<m;i++)
add_edge(S,i+*na,);
for(int i=;i<na;i++)
add_edge(i,i+na,);
for(int i=;i<=m;i++)
add_edge((na-i)+na,T,);
for(int i=;i<nb;i++)
add_edge(i+na+na,i+na+na+nb,);
for(int i=;i<na;i++)
for(int j=;j<nb;j++)
if(abs(ha[i]-hb[j])<t){
add_edge(i+na,j+*na,);
add_edge(j+*na+nb,i,);
}
f=max_flow(S,T);
if (f >= m)
cout << "S" << endl;
else
cout << "N" << endl;
return ;
}
Codeforces Gym 100733I The Cool Monkeys 拆点+最大流的更多相关文章
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
- CodeForces Gym 100213F Counterfeit Money
CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- hdu4289 最小割最大流 (拆点最大流)
最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...
随机推荐
- Python中的属性访问与描述符
Python中的属性访问与描述符 请给作者点赞--> 原文链接 在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个nam ...
- bs4--官文--搜索文档树
搜索文档树 Beautiful Soup定义了很多搜索方法,这里着重介绍2个: find() 和 find_all() .其它方法的参数和用法类似,请读者举一反三. 再以“爱丽丝”文档作为例子: ht ...
- 《鸟哥的Linux私房菜》学习笔记(1)——文件与目录
在Linux中,任何设备都是文件,不仅如此,连数据通信的接口也有专门的文件负责.可以说,一切皆文件,目录也是一种文件,是路径映射.因此,文件系统是Linux的基础. 一.文件与目录管理命令 1.ls( ...
- ROM,PROM,EPROM,EEPROM及FLASH存储器的区别
在微机的发展初期,BIOS都存放在ROM(Read Only Memory,只读存储器)中.ROM内部的资料是在ROM的制造工序中,在工厂里用特殊的方法被烧录进去的,其中的内容只能读不能改,一旦烧录进 ...
- 关于ios 和 android 录音(语音)对聊文件格式问题
关于ios 和 android 录音(语音)对聊文件格式问题 在做语音对讲的时候,将会碰到录制语音格式的问题,这些需要跨平台我们可能需要使用双方平台都支持的格式,或者执行编码转换 解决方式如下: wa ...
- 了解JavaScript核心精髓(一)
ES5 1.声明脚本 <script type="text/javascript"></script> 2.DOM与BOM DOM(Document Obj ...
- webdriver高级应用- 操作富文本框
富文本框的技术实现和普通的文本框的定位存在较大的区别,富文本框的常见技术用到了Frame标签,并且在Frame里面实现了一个完整的HTML网页结构,所以使用普通的定位模式将无法直接定位到富文本框对象. ...
- 九度oj 题目1494:Dota
题目描述: 大家都知道在dota游戏中,装备是对于英雄来说十分重要的要素. 英雄们不仅可以购买单个的装备,甚至某些特定的装备组合能够合成更强的装备. 为了简化问题,我们将每个装备对于英雄的功能抽象为一 ...
- 利用MVC模式简单设计jsp分页效果
利用Mysql创建一个表Car 用Eclipse创建一个Dynamic Web Project 在lib目录下导入Mysql的jar包 创建如下文件 package com.bean; public ...
- iOS学习笔记31-从图册获取图片和视频
一.从图册中获取本地图片和视频 从图册中获取文件,我们使用的是UIImagePickerController,这个类我们在之前的摄像头中使用过,这里是链接:iOS学习笔记27-摄像头,这里我们使用的是 ...