Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门
题目大意
$m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\right\rfloor$个巢穴连通。第$i$个巢穴在最终时允许$c_i$只醒来的鼹鼠最终停留在这。已知第$i$只鼹鼠在第$p_i$个巢穴睡觉。要求求出对于每个满足$1 \leqslant k \leqslant n$的$k$,如果前$k$只鼹鼠醒来,最小的移动距离的总和。
考虑费用流的建图和暴力做法,把原图的边容量设为无限大,费用设为1(每条无向边要拆成两条),每个点再向汇点连一条边,容量为$c_i$,费用为0。
对于每次源点向$p_i$增广1单位的流量。
显然这样会超时,考虑优化费用流。
显然有:
- 每次增广的路径一定是一条简单路径
- 对于原图的一条无向边,它两个方向的边不会同时有流量(走另外一条弧的反向弧显然可以减少费用)
那么每次枚举路径的LCA,对于每个点记录一下$f_i$表示从$i$走到子树内的任意一个点的最短距离。
显然每次更新边权后很容易能够维护$f$。时间复杂度$O(n\log n)$。
Code
/**
* Codeforces
* Gym#101190M
* Accepted
* Time: 108ms
* Memory: 2200k
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean; #define ll long long const signed ll llf = (signed ll) (~0ull >> );
const signed int inf = (signed) (~0u >> );
const int N = ; #define pii pair<int, int> pii operator + (pii a, int b) {
return pii(a.first + b, a.second);
} int n, m;
int w[N], c[N];
pii fd[N]; inline void init() {
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++) {
scanf("%d", c + i);
}
} int value(int p, int dir) { // up : +1, down : -1
int prod = w[p] * dir;
return (prod >= ) ? () : (-);
} void __update(int p) {
fd[p] = pii(inf * (!c[p]), p);
if ((p << ) <= n && fd[p << ].first != inf)
fd[p] = min(fd[p], fd[p << ] + value(p << , -));
if ((p << ) < n && fd[p << | ].first != inf)
fd[p] = min(fd[p], fd[p << | ] + value(p << | , -));
} int update(int s) {
int val = fd[s].first, g = s, v = fd[s].second, len = ;
for (int p = s, d = (p & ), q, cmp; len += value(p, ), p >>= ; d = p & ) {
q = p << | (d ^ );
if (q <= n && (cmp = fd[q].first + value(q, -) + len) < val)
val = cmp, g = p, v = fd[q].second;
if (c[p] && len < val)
val = len, g = v = p;
}
c[v]--;
for (int p = s; p != g; p >>= ) {
w[p]++;
__update(p);
}
for (int p = v; p != g; p >>= ) {
w[p]--;
__update(p);
}
for (int p = g; p; p >>= )
__update(p);
// cerr << s << " " << v << '\n';
return val;
} inline void solve() {
for (int i = ; i <= n; i++)
fd[i] = pii(inf * (!c[i]), i);
for (int i = n; i > ; i--)
fd[i >> ] = min(fd[i >> ], fd[i] + ); int x;
ll res = ;
while (m--) {
scanf("%d", &x);
res += update(x);
printf(Auto" ", res);
}
} int main() {
freopen("mole.in", "r", stdin);
freopen("mole.out", "w", stdout);
init();
solve();
return ;
}
Codeforces Gym 101190M Mole Tunnels - 费用流的更多相关文章
- Codeforces 362E Petya and Pipes 费用流建图
题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...
- Codeforces Gym 100203I I WIN 最大流
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 首先寻找每个I,然后枚举形状,如果匹 ...
- CodeForces - 884F :Anti-Palindromize(贪心&费用流)
A string a of length m is called antipalindromic iff m is even, and for each i (1 ≤ i ≤ m) ai ≠ am - ...
- Codeforces Gym 100002 E "Evacuation Plan" 费用流
"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- 【BZOJ4849】[Neerc2016]Mole Tunnels 模拟费用流
[BZOJ4849][Neerc2016]Mole Tunnels Description 鼹鼠们在底下开凿了n个洞,由n-1条隧道连接,对于任意的i>1,第i个洞都会和第i/2(取下整)个洞间 ...
- BZOJ4849[Neerc2016]Mole Tunnels——模拟费用流+树形DP
题目描述 鼹鼠们在底下开凿了n个洞,由n-1条隧道连接,对于任意的i>1,第i个洞都会和第i/2(取下整)个洞间有一条隧 道,第i个洞内还有ci个食物能供最多ci只鼹鼠吃.一共有m只鼹鼠,第i只 ...
- bzoj 4849: [Neerc2016]Mole Tunnels【模拟费用流】
参考:https://www.cnblogs.com/CQzhangyu/p/6952371.html 费用流很简单,考虑但是会T. 考虑费用流的本质,流一次需要要找一个能够从当前点到达的距离最小的点 ...
- BZOJ 4849 [NEERC2016]Mole Tunnels (模拟费用流)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4849 题解 其实也是模拟费用流,但是这道题和一般的题目不一样,这道题是在一个完全二叉树上 ...
- P6122-[NEERC2016]Mole Tunnels【模拟费用流】
正题 题目链接:https://www.luogu.com.cn/problem/P6122 题目大意 给出\(n\)个点的一棵满二叉树,每个点有容量\(c_i\),\(m\)次从\(p_i\)处加一 ...
随机推荐
- day01 python入门之路
Python之路,Day1 - Python基础1 本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼 ...
- HttpServlet
HttpServlet的原理 HttpServlet抽象类中的(部分)方法 HttpServlet extends GenericServlet{ void service(ServletReques ...
- 2019年春季学期第二周作业 基础作业 请在第一周作业的基础上,继续完成:找出给定的文件中数组的最大值及其对应的最小下标(下标从0开始)。并将最大值和对应的最小下标数值写入文件。 输入: 请建立以自己英文名字命名的txt文件,并输入数组元素数值,元素值之间用逗号分隔。 输出 在不删除原有文件内容的情况下,将最大值和对应的最小下标数值写入文件
~~~ include<stdio.h> include<stdlib.h> int main() { FILE*fp; int i=0,max=0,j=0,maxb=0; i ...
- promise 的学习
promise 是为了解决异步操作的顺序问题而产生的 特性 promise 的实例一旦创建就会执行里面的异步操作 promise 的实例状态一旦改变就变成凝固的了, 无法再对其作出修改, (不明白为 ...
- 严重:one or more listeners failed. Full details will be found in the appropriate container log file
one or more listeners failed. Full details will be found in the appropriate container log file 这句话 ...
- 使用vue-cli3搭建一个项目
前面说过用vue-cli3快速开发原型的搭建,下面来说一下搭建一个完整的项目 首先我们可以输入命令(创建一个项目名为test的项目) vue create test 输完这个命令后,会让你选择配置项, ...
- Get Random number
, int pMaxVal = int.MaxValue) { int m = pMaxVal - pMinVal; int rnd = int.MinValue; decimal _base = ( ...
- 【Solution】MySQL 5.8 this is incompatible with sql_mode=only_full_group_by
[42000][1055] Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated colu ...
- 资讯类产品-创业邦APP产品原型模板公开分享
众所周知,知识付费和资讯是近年来两个受关注度极高的互联网产品方向.18年喜马拉雅“123狂欢节”,3天时间内容消费额4.35亿,足见知识付费内容市场的火爆.字节跳动凭借今日头条APP起家,逐渐跻身互联 ...
- PHP fwrite 函数:将字符串写入文件(追加与换行)
PHP fwrite() fwrite() 函数用于向文件写入字符串,成功返回写入的字符数,否则返回 FALSE . 语法: int fwrite( resource handle, string s ...