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和其整个子树都将被删去,求被 ...
随机推荐
- js random获取随机数,获取任意范围内随机整数
壹 ❀ 引 想着好久没做笔试题了,去GitHub找了面试相关的项目,结果被第一道题难住了.....说难其实也不难,而是我忘记了取范围随机整数怎么写了,不可否认如果当时是我在笔试,肯定也凉了,那么就由 ...
- Windows环境部署Redis集群
一.准备文件 1. 下载Redis for windows 的最新版本 下载地址:https://github.com/MSOpenTech/redis/releases 安装到 c:\Redis ...
- Excel中vlookup函数使用
https://baijiahao.baidu.com/s?id=1594684818733205984&wfr=spider&for=pc
- PCB SQL SERVER 数据库阻塞进程关系以思维导图方式呈现的实现方法
最近公司服务数据库同步机制常发生阻塞,时不时的导致PCB工程系统卡死现象,只有找到阻塞源头并处理掉,才以消除阻塞,但数据库中查看会话阻塞是通过二维表方式展示的父子会话进程ID的,是很难清楚的展示各会话 ...
- Windows Presentation Foundation (WPF) 项目中不支持xxx的解决
一般Windows Presentation Foundation (WPF) 项目中不支持xxx都是由于没引用相应的程序集导致,比如Windows Presentation Foundation ( ...
- 洛谷P2299 Mzc和体委的争夺战 题解
题目 题目描述 mzc家很有钱(开玩笑),他家有n个男家丁(做过前三弹的都知道).但如此之多的男家丁吸引来了我们的体委(矮胖小伙),他要来与mzc争夺男家丁. mzc很生气,决定与其决斗,但cat的体 ...
- [USACO07FEB]银牛派对Silver Cow Party
题目简叙: 寒假到了,N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100). 每头牛参加 ...
- 基站搭建与IMSI捕获
写在前面 : 实验目的是为了教学交流,坚决抵制违法行为. 一.实验目的 搭建基于OpenBTS的基站,手机接入该基站,进行短信.语音等测试. 二.所用仪器 USRP B210 1台,天线2根,PC机 ...
- “朕赐给你,才是你的;朕不给,你不能抢”--custome role在Azure权限管理中的简单实践
在开始详细讨论技术问题之前,有一些个人观点想发表一下: ---作为一个甲方云平台的掌控着,如果任何事情你都是让partner全部帮你搞定,自己既不审核也不研究,那无论是对于公司还是个人发展来说都是没任 ...
- Java创建线程的两个方法
Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...