题意简述

给你一颗有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的更多相关文章

  1. 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  2. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  3. codeforces 220 C. Game on Tree

    题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...

  4. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  6. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. codeforces 342E :Xenia and Tree

    Description Xenia the programmer has a tree consisting of n nodes. We will consider the tree nodes i ...

  8. 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 ...

  9. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

随机推荐

  1. MySQL 8.0 information_schema系统库的改进

    目录 information_schema有何用? mysql8.0 之前的查询方式 mysql8.0 开始的查询方式 测试5.7和8.0不同版本访问I_S库的性能 结论 information_sc ...

  2. python接口自动化(二十九)--html测试报告通过邮件发出去——上(详解)

    简介 前边几篇,已经教小伙伴们掌握了如何生成HTML的测试报告,那么生成测试报告,我们也不能放在那里不管了,这样即使你报告在漂亮,领导也看不到.因此如果想向领导汇报工作,不仅需要提供更直观的测试报告. ...

  3. JavaScript入门小案例

    笔记: <!-- JavaScript的特点: 1.基于对象和事件驱动 JavaScript把HTML页面中的每一个元素都当做一个对象来处理,并且这些对象都具有层次关系, 像一颗倒立的树,这种关 ...

  4. TF项目实战(SSD目标检测)-VOC2007

    TF项目实战(SSD目标检测)-VOC2007 训练好的模型和代码会公布在网上: 步骤: 1.代码地址:https://github.com/balancap/SSD-Tensorflow 2.解压s ...

  5. Flink实战(六) - Table API & SQL编程

    1 意义 1.1 分层的 APIs & 抽象层次 Flink提供三层API. 每个API在简洁性和表达性之间提供不同的权衡,并针对不同的用例. 而且Flink提供不同级别的抽象来开发流/批处理 ...

  6. html+css-->background-img(背景图的设置)

    背景图:(相关验证代码请查看代码,在验证时需将当前不需要验证的代码注释掉)    1.inherit:从父元素继承属性设置    2.background-repeat:平铺(在图片大小小于元素尺寸时 ...

  7. c++课程设计:行政区划管理系统

    大一的课程设计基本上除了计算器,就是各种管理系统.(大概吧) 感觉看到题目整个一年的c++好像没学明白似的.基础知识掌握还算可以,真刀真枪的打代码,而且是实现这么些功能,做成一个管理系统,就真正感觉到 ...

  8. python执行unittest界面设置

    执行单元测试时,系统会自动添加unittest in...的执行服务器. 执行时unittest in...的执行服务器在界面右上方可以看到,且执行结果为左侧框和右侧统计结果. 如果没有,会导致测试结 ...

  9. pyqt 主程序运行中处理其他事件(多线程的一种代替方式)

    一.实验环境 1.Windows7x64_SP1 2.Anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 4.通过Anacond ...

  10. 给hexo博客的NEXT主题添加一个云日历

    一点废话 hexo中有文件的归档,但是博文的数目多了,浏览的时候也是很不方便的.于是我就有找个云日历的想法了,折腾了几天,网上的方法都试过了.但是没出效果.于是想着自己来写一个.这自己写的这部分是基于 ...