Codeforces 868E Policeman and a Tree
题意简述
给你一颗有n个点的树,每条边有边权,有一个警察一开始在点S,他的速度是1,即通过一条长度为x的边要花x单位时间。
有m个罪犯,一开始第i个在点x[i],他们的速度无限快。
如果罪犯和警察到达同一个点,那么罪犯会被抓住。
现在罪犯们想最大化最后一个被抓的时间,警察想最小化抓的时间。
求警察抓住所以罪犯的时间的最小值
题解思路
dp
dp[u][v][x][y] 表示从u走向v,以v为根的子树有x个罪犯,其余有y个罪犯
然后进行转移
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
const int INF = 0x3f3f3f3f;
int n, m, s, u, v, w, tmp, cnt, ans = INF, xx;
int h[60], nxt[120], to[120], c[60], sz[60];
int dis[60][60], f[2][60];
int dp[60][60][60][60];
inline mmin(int& x, const int& y) {if (x > y) x = y; }
inline mmax(int& x, const int& y) {if (x < y) x = y; }
inline void add_edge(const int& u, const int& v)
{
to[++cnt] = v;
nxt[cnt] = h[u];
h[u] = cnt;
}
void dfs(const int& u, const int& fa)
{
for (register int i = h[u]; i; i = nxt[i])
if (to[i] != fa)
{
dfs(to[i], u);
c[u] += c[to[i]];
}
}
int solve(const int& u, const int& v, const int& x, const int& y)
{
if (!x && !y) return 0;
if (!x) return INF;
if (dp[u][v][x][y] < INF) return dp[u][v][x][y];
if (sz[v] == 1) return (dp[u][v][x][y] = (solve(v, u, y, 0) + dis[u][v]));
for (register int i = h[v]; i; i = nxt[i])
if (to[i] != u)
for (register int j = 0; j <= x; ++j)
solve(v, to[i], j, x + y - j);
memset(f[1], 0, sizeof(f[1]));
f[tmp = 1][0] = INF;
for (register int i = h[v]; i; i = nxt[i])
if (to[i] != u)
{
tmp ^= 1;
memset(f[tmp], 0, sizeof(f[tmp]));
for (register int j = 0; j <= x; ++j)
for (register int k = 0; j + k <= x; ++k)
{
xx = solve(v, to[i], k, x + y - k);
mmax(f[tmp][j + k], std::min(f[tmp ^ 1][j], xx));
}
}
return (dp[u][v][x][y] = (f[tmp][x] + dis[u][v]));
}
int main()
{
scanf("%d", &n);
for (register int i = 1; i < n; ++i)
{
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v); add_edge(v, u);
dis[u][v] = dis[v][u] = w;
++sz[u]; ++sz[v];
}
scanf("%d%d", &s, &m);
for (register int i = 1; i <= m; ++i) { scanf("%d", &tmp); ++c[tmp]; }
dfs(s, 0);
memset(dp, INF, sizeof(dp));
for (register int i = h[s]; i; i = nxt[i])
mmin(ans, solve(s, to[i], c[to[i]], m - c[to[i]]));
printf("%d\n", ans);
}
Codeforces 868E Policeman and a Tree的更多相关文章
- codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 379 F. New Year Tree
\(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 342E :Xenia and Tree
Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes i ...
- Codeforces Edu3 E. Minimum spanning tree for each edge
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
随机推荐
- 深入学习Spring框架(三)- AOP面向切面
1.什么是AOP? AOP为 Aspect Oriented Programming 的缩写,即面向切面编程, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术..AOP是OOP的延续, ...
- [开源]OSharpNS 步步为营系列 - 3. 添加业务服务层
什么是OSharp OSharpNS全称OSharp Framework with .NetStandard2.0,是一个基于.NetStandard2.0开发的一个.NetCore快速开发框架.这个 ...
- 006.SQLServer AlwaysOn可用性组高可用部署
一 数据库镜像部署准备 1.1 数据库镜像支持 有关对 SQL Server 2012 中的数据库镜像的支持的信息,请参考:https://docs.microsoft.com/zh-cn/previ ...
- 通讯(tarjan缩点)(20190716NOIP模拟测试4)
B. 通讯 题目类型:传统 评测方式:文本比较 内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目描述 “这一切都是命运石之门的选择.” 试图研制时间机器的机关SERN截获了 ...
- Java学习笔记之---集合
Java学习笔记之---集合 (一)集合框架的体系结构 (二)List(列表) (1)特性 1.List中的元素是有序并且可以重复的,成为序列 2.List可以精确的控制每个元素的插入位置,并且可以删 ...
- 快速掌握mongoDB(二)——聚合管道和MapReduce
上一节简单介绍了一下mongoDB的增删改查操作,这一节将介绍其聚合操作.我们在使用mysql.sqlserver时经常会用到一些聚合函数,如sum/avg/max/min/count等,mongoD ...
- stixel_world+Multi_stioxel_world+semantic_stixel_world知识拓展
Semantic_Stixel_World 学习笔记 因项目方向更改,该研究暂停, 转为opengl等3D渲染. Author: Ian 星期四, 20. 六月 2019 06:11下午 最近看网络上 ...
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...
- 微信小程序踩坑日记1——调用微信授权窗口
0. 引言 微信小程序为了优化用户体验,取消了在进入小程序时立马出现授权窗口.需要用户主动点击按钮,触发授权窗口. 那么,在我实践过程中,出现了以下问题. . 无法弹出授权窗口 . 希望在用户已经授权 ...
- 我狠起来连自己都打---如何简单实现Azure resource自动打标签
你是否还在为花费大量Azure Resource打标签而烦恼呢?你是否还在因为这样低效的重复劳动而痛苦呢? 在很长一段时间内,笔者既要做云架构调整,又要做日常系统维护,还要参与各种各样的项目,在这种情 ...