SGU438_The Glorious Karlutka River =)
好题,有一些人在河的一边,想通过河里的某些点跳到对岸去。每个点最多只能承受一定数量的人,每人跳跃一次需要消耗一个时间。求所有人都过河的最短时间。
看网上说是用了什么动态流的神奇东东。其实就是最大流吧,不过是一个很有意思的模型。
每递增一个时间,所有的点增加一层,因为有的人可以站在上一个点不走动,最终每个点分别表示河中的某个点在某个特定的时刻。
同时为了保证人数在点的承受范围之内,拆点即可。
一直增加层数,直到最大流达到m为止即可。
召唤代码君:
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 55555
#define maxm 9999999
using namespace std; int to[maxm],next[maxm],c[maxm],first[maxn],edge;
int Q[maxm],bot,top,node;
int tag[maxn],d[maxn],TAG=;
int L[],R[];
bool can[maxn],iq[maxn];
int X[],Y[],C[],connect[][];
int n,m,D,W,s,t,ans; int addnode()
{
first[++node]=-;
return node;
} void addedge(int U,int V,int W)
{
edge++;
to[edge]=V,c[edge]=W,next[edge]=first[U],first[U]=edge;
edge++;
to[edge]=U,c[edge]=,next[edge]=first[V],first[V]=edge;
} bool _input()
{
bot=,top=;
scanf("%d%d%d%d",&n,&m,&D,&W);
for (int i=; i<=n; i++)
{
iq[i]=false;
scanf("%d%d%d",&X[i],&Y[i],&C[i]);
if (C[i]==)
{
i--,n--;
continue;
}
if (Y[i]<=D) Q[++top]=i,iq[i]=true;
}
memset(connect,false,sizeof connect);
for (int i=; i<=n; i++)
for (int j=; j<=n; j++)
if (i!=j && (X[i]-X[j])*(X[i]-X[j])+(Y[i]-Y[j])*(Y[i]-Y[j])<=D*D)
connect[i][j]=connect[j][i]=true;
while (bot<=top)
{
int cur=Q[bot++];
if (Y[cur]+D>=W) return true;
for (int i=; i<=n; i++)
if (connect[cur][i] && !iq[i]) Q[++top]=i,iq[i]=true;
}
if (D<W) return false;
else return true;
} void build_init_graph()
{
edge=-,node=;
s=addnode(),t=addnode();
for (int i=; i<=n; i++) L[i]=addnode(),R[i]=addnode();
for (int i=; i<=n; i++)
{
addedge(L[i],R[i],C[i]);
if (Y[i]<=D) addedge(s,L[i],C[i]);
if (Y[i]+D>=W) addedge(R[i],t,C[i]);
}
} bool bfs()
{
Q[bot=top=]=t,d[t]=,tag[t]=++TAG,can[t]=false;
while (bot<=top)
{
int cur=Q[bot++];
for (int i=first[cur]; i!=-; i=next[i])
if (c[i^]> && tag[to[i]]!=TAG)
{
tag[to[i]]=TAG,d[to[i]]=d[cur]+;
can[to[i]]=false,Q[++top]=to[i];
if (to[i]==s) return true;
}
}
return false;
} int dfs(int cur,int num)
{
if (cur==t) return num;
int tmp=num,k;
for (int i=first[cur]; i!=-; i=next[i])
if (c[i]> && d[to[i]]==d[cur]- && tag[to[i]]==TAG && !can[to[i]])
{
k=dfs(to[i],min(num,c[i]));
if (k) num-=k,c[i]-=k,c[i^]+=k;
if (num==) break;
}
if (num) can[cur]=true;
return tmp-num;
} int maxflow()
{
int tot=;
while (bfs()) tot+=dfs(s,maxm);
return tot;
} int main()
{
if (!_input()) { puts("IMPOSSIBLE"); return ; }
if (D>=W) { puts(""); return ; }
build_init_graph();
for (ans=; maxflow()<m; ans++)
{
for (int i=; i<=edge; i+=) if (c[i]>) c[i-]+=c[i],c[i]=;
for (int i=; i<=n; i++)
{
L[i]=addnode();
if (Y[i]<=D) addedge(s,L[i],C[i]);
for (int j=; j<=n; j++)
if (connect[i][j]) addedge(R[j],L[i],C[i]);
}
for (int i=; i<=n; i++)
{
R[i]=addnode();
addedge(L[i],R[i],C[i]);
if (Y[i]+D>=W) addedge(R[i],t,C[i]);
}
}
printf("%d\n",ans);
return ;
}
SGU438_The Glorious Karlutka River =)的更多相关文章
- SGU 438 The Glorious Karlutka River =)(最大流)
Description A group of Mtourists are walking along the Karlutka river. They want to cross the river, ...
- SGU438 The Glorious Karlutka River =)(最大流)
题目大概说有m个人要过一条宽W的河,人最远跳远距离是d,河上有n个垃圾堆,每个垃圾堆都有坐标和同一时间能容纳的人数,问所有人最少要跳几次才能跳到对岸. 又是一题根据时间拆点的最大流. 二分时间建容量网 ...
- SGU 438 The Glorious Karlutka River =) ★(动态+分层网络流)
[题意]有一条东西向流淌的河,宽为W,河中有N块石头,每块石头的坐标(Xi, Yi)和最大承受人数Ci已知.现在有M个游客在河的南岸,他们想穿越这条河流,但是每个人每次最远只能跳D米,每跳一次耗时1秒 ...
- The Glorious Karlutka River =)
sgu438:http://acm.sgu.ru/problem.php?contest=0&problem=438 题意:有一条东西向流淌的河,宽为 W,河中有 N 块石头,每块石头的坐标( ...
- SGU 0438 The Glorious Karlutka River =) 动态流
题目大意:有一条东西向流淌的河,宽为W,河中有N块石头,每块石头的坐标(Xi, Yi)和最大承受人数Ci已知.现在有M个游客在河的南岸,他们想穿越这条河流,但是每个人每次最远只能跳D米,每跳一次耗时1 ...
- SGU438 The Glorious Karlutka River =)
传送门 sgu原来搬到cf了呀点了好几个链接才找到233 传说中的动态流(?) 反正很暴力就对了QwQ 有容量限制->拆点 对于每个点拆成入点和出点 时间限制->分层 对于每个时刻的每个石 ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...
- Moon River
读书笔记系列链接地址http://www.cnblogs.com/shoufengwei/p/5714661.html. 昨晚无意中听到了一首英文歌曲,虽不知其意,但是瞬间就被优美的旋律 ...
- poj[3093]Margaritas On River Walk
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
随机推荐
- Spring学习----- Spring配置文件xml文档的schema约束
1.配置文件示例. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- 动态权限<三>华为小米特殊机制
动态权限对于谷歌来说从android6.0引入,对于国内的rom来说,这个题目不是好的选择题.因为大多数时候由于使用群众的层次不同,有些人在乎隐私的泄露,而更多的人却并不关心,使用了动态权限,增加了用 ...
- sublime text 安装json插件
1.安装JSON插件(直接安装或在线插件安装均可) 1-1.直接安装 下载安装包https://github.com/dzhibas/SublimePrettyJson,解压缩到Packages目录( ...
- TensorFlow Python3.7环境下的源码编译(三)编译
这里要为仅支持 CPU 的 TensorFlow 构建一个 pip 软件包,需要调用以下命令: $ bazel build --cxxopt="-D_GLIBCXX_USE_CXX11_AB ...
- 一个可以自由存取的onedriver
https://cittedu-my.sharepoint.com/personal/jostin_5gd_me/Documents/jostin
- JAVA之访问控制符
1.访问修饰符 public:该类和非该类的均能访问 protect:该类和该类的子类,同一个包内的成员也能访问 默认:同一个包内的类可以访问 private:只有该类可以访问 特性:在继承的关系中, ...
- js多条件if语句简写发生Uncaught SyntaxError: Unexpected token }
改写原生js 多条件if判断语句时,采用三元方法,发生Uncaught SyntaxError: Unexpected token } function compareImgSize() { var ...
- MCS锁——可伸缩的自旋锁
在编写并发同步程序的时候,如果临界区非常小,比如说只有几条或几十条指令,那么我们可以选择自旋锁(spinlock).使用普通的互斥锁会涉及到操作系统的调度,因此小临界区一般首选自旋锁.自旋锁的工作方式 ...
- python中__name__属性的使用
python常用模块目录 1.打印出函数名字而非函数名对应的地址 )打印的是函数地址 def func(): print("我是%s函数"%func) func() ------- ...
- Ubuntu16.04Server版离线安装Nginx1.8.1+Mysql5.7.23+Python3.6.2
nginx1.8.1 1.安装前准备工作 1.1.检查系统版本,确认源码编译所依赖的环境,提前下载好压缩包. 整个环境都是使用root权限安装,系统版本为server版的ubuntu16.04.4 r ...