「JSOI2008」Blue Mary的旅行
传送门
Luogu
解题思路
分层图加网络流,有点像这题
可以证明最多不超过100天,所以才可以分层,不然图的规模会很大。
首先连源点汇点: \((s,1,INF), (n, t, INF)\)
以时间分层,每次把原图中的边 \((u, v, w)\) 改为一条 \((u_{day1}, v_{day2}, w)\) 的弧。
对于 \(u < n\),连一条弧 \((u_{day1}, u_{day2}, INF)\),以及一条 \((n_{day2}, n_{day1}, INF)\)。
然后每次在残量网络上加边增广直至最大流大于等于 \(T\)。
细节注意事项
- 每次增广时都要把流量累加
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 5010;
const int __ = 250010;
const int INF = 2147483647;
int tot = 1, head[_], nxt[__ << 1], ver[__ << 1], cap[__ << 1];
inline void Add_edge(int u, int v, int d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, cap[tot] = d; }
inline void link(int u, int v, int d) { Add_edge(u, v, d), Add_edge(v, u, 0); }
int n, m, k, s, t, dep[_], cur[_];
struct node { int u, v, d; } x[2452];
inline int id(int u, int d) { return u + (d - 1) * n; }
inline int bfs() {
static queue < int > Q;
memset(dep, 0, sizeof dep);
dep[s] = 1, Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (dep[v] == 0 && cap[i] > 0)
dep[v] = dep[u] + 1, Q.push(v);
}
}
return dep[t] > 0;
}
inline int dfs(int u, int flow) {
if (u == t) return flow;
for (rg int& i = cur[u]; i; i = nxt[i]) {
int v = ver[i];
if (dep[v] == dep[u] + 1 && cap[i] > 0) {
int res = dfs(v, min(flow, cap[i]));
if (res) { cap[i] -= res, cap[i ^ 1] += res; return res; }
}
}
return 0;
}
inline int Dinic(int day) {
int res = 0;
while (bfs()) {
cur[s] = head[s], cur[t] = head[t];
for (rg int x = 1; x <= day; ++x)
for (rg int i = 1; i <= n; ++i)
cur[id(i, x)] = head[id(i, x)];
while (int d = dfs(s, INF)) res += d;
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m), read(k);
for (rg int i = 1; i <= m; ++i)
read(x[i].u), read(x[i].v), read(x[i].d);
s = _ - 1, t = _ - 2;
link(s, id(1, 1), INF);
link(id(n, 1), t, INF);
int res = 0;
for (rg int d = 2; ; ++d) {
for (rg int i = 1; i <= m; ++i)
link(id(x[i].u, d - 1), id(x[i].v, d), x[i].d);
for (rg int i = 1; i < n; ++i)
link(id(i, d - 1), id(i, d), INF);
link(id(n, d), id(n, d - 1), INF);
res += Dinic(d);
if (res >= k) { printf("%d\n", d - 1); return 0; }
}
return 0;
}
完结撒花 \(qwq\)
「JSOI2008」Blue Mary的旅行的更多相关文章
- 【BZOJ1570】[JSOI2008]Blue Mary的旅行 动态加边网络流
[BZOJ1570][JSOI2008]Blue Mary的旅行 Description 在一段时间之后,网络公司终于有了一定的知名度,也开始收到一些订单,其中最大的一宗来自B市.Blue Mary决 ...
- bzoj1570: [JSOI2008]Blue Mary的旅行(二分+网络流)
1570: [JSOI2008]Blue Mary的旅行 题目:传送门 题解: get到拆点新姿势,还是做题太少了...ORZ 因为每天就只能有一个航班,那就不能直接连了,所以要拆点(然后就被卡住了) ...
- bzoj 1570: [JSOI2008]Blue Mary的旅行
Description 在一段时间之后,网络公司终于有了一定的知名度,也开始收到一些订单,其中最大的一宗来自B市.Blue Mary决定亲自去签下这份订单.为了节省旅行经费,他的某个金融顾问建议只购买 ...
- [JSOI2008] [BZOJ1567] Blue Mary的战役地图 解题报告 (hash)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1567 Description Blue Mary最近迷上了玩Starcraft(星际争霸 ...
- BZOJ1570 [JSOI2008]Blue Mary的旅行
建分层图,每一层表示一天的情况 从S向第0层的1号点连边,每层的n向T连INF的边 枚举天数,每多一天就多建一层然后跑最大流,如果当前流量大于人数则输出答案 由于路径长度不会超过n,因此tot个人走这 ...
- BZOJ 1570: [JSOI2008]Blue Mary的旅行( 二分答案 + 最大流 )
二分答案, 然后对于答案m, 把地点分成m层, 对于边(u, v), 第x层的u -> 第x+1层的v 连边. 然后第x层的u -> 第x+1层的u连边(+oo), S->第一层的1 ...
- [JSOI2008]Blue Mary的旅行
嘟嘟嘟 看\(n\)那么小,就知道是网络流.然后二分,按时间拆点. 刚开始我看成所有航班一天只能起飞一次,纠结了好一会儿.但实际上是每一个航班单独考虑,互不影响. 建图很显然,拆完点后每一个点的第\( ...
- 【bzoj1507】 JSOI2008—Blue Mary的旅行
http://www.lydsy.com/JudgeOnline/problem.php?id=1570 (题目链接) 题意 给出$m$个航班,每天只能做一次飞机,有$T$人从起点到终点,问最晚到达的 ...
- LG1198/BZOJ1012 「JSOI2008」最大数 线段树+离线
问题描述 LG1198 BZOJ1012 题解 我们把所有操作离线,设一共有\(n\)个插入操作. 于是提前建立\(n\)个数,全部设为\(-INF\) 接着逐个处理操作即可. \(\mathrm{C ...
随机推荐
- pl/sql修改data
1,对于语句要包含rowid!
- 【转载】IntelliJ IDEA配置JUnit进行单元测试
前提条件 安装JDK,并配置好环境变量 工程已解决JUnit依赖关系(pom.xml) IDEA中JUnit配置 IDEA自带一个JUnit插件,打开Settings窗口搜索junit,如图: 图 ...
- [运维] 请求 nginx 出现 502 Bad Gateway 的解决方案!
环境: 云服务器镜像 Linux CentOS 7.6 已经安装并成功配置 SSL 的 nginx 1.16.1 成功安装并且可以正常运行的 apache-tomcat-9.0.26 遇到的问题: 在 ...
- TortoiseGit 安装与配置
2. TortoiseGit安装与配置 标签: TortoiseGit安装配置Windows 2014-12-01 15:25 135739人阅读 评论(10) 收藏 举报 .embody{ padd ...
- jquery移除click事件
原文链接:https://blog.csdn.net/weixin_41228949/article/details/83142661 在html中定义click事件有两种方式,针对这两种方式有两种移 ...
- Matlab的简单数据保存读取
1.使用load进行文件读取 例如读入文件名为'filename.txt'中的数据,那么可以使用以下代码: load('filename.txt') 注意:filename.txt中的数据应符合矩阵形 ...
- 攻防世界Web进阶-Upload1
进入题目 题目提示上传文件,准备好一句话木马. 发现只能上传图片文件,那么再准备一个图片的一句话木马.上传. 浏览器开代理,使用burpsuite拦截,修改文件后缀名为php,否则无法连接 使用蚁剑连 ...
- tomcat注册为windows服务
打开CMD,进入到Tomcat的bin目录,执行命令:service.bat install [service_name] 如果卸载服务,可以执行:sc delete [service_name]
- gensim加载词向量文件
# -*- coding: utf-8 -*- # author: huihui # date: 2020/1/31 7:58 下午 ''' 根据语料训练词向量,并保存向量文件 ''' import ...
- 【PAT甲级】1027 Colors in Mars (20 分)
题意: 输入三个范围为0~168的整数,将它们从十三进制转化为十进制然后前缀#输出. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...