CodeForces 868 E. Policeman and a Tree
来源:互联网 发布:手机淘宝买家秀没有了 编辑:程序博客网 时间:2024/06/15 14:10
链接:
link
题意:
一棵
题解:
转移做背包。
代码:
#include <bits/stdc++.h>#define xx first#define yy second#define mp make_pair#define pb push_back#define mset(x, y) memset(x, y, sizeof x)#define mcpy(x, y) memcpy(x, y, sizeof x)using namespace std;typedef long long LL;typedef pair <int, int> pii;inline int Read(){ int x = 0, f = 1, c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f;}const int MAXN = 55;const int INF = 0x3f3f3f3f;int n, m, s, siz[MAXN], val[MAXN][MAXN], g[MAXN][MAXN], f[MAXN][MAXN][MAXN][MAXN];vector <int> adj[MAXN], trans[MAXN][MAXN];inline void Dfs(int x, int p){ for (auto y : adj[x]) if (y ^ p) Dfs(y, x), siz[x] += siz[y];}inline int Dp(int p, int x, int cnt, int tot){ if (!tot) return 0; if (!cnt) return INF; if (f[p][x][cnt][tot] ^ INF) return f[p][x][cnt][tot]; if (trans[p][x].empty()) return f[p][x][cnt][tot] = val[p][x] + Dp(x, p, tot - cnt, tot - cnt); for (auto y : trans[p][x]) for (int i = 0; i <= cnt; i ++) Dp(x, y, i, tot); for (int i = 0; i <= trans[p][x].size(); i ++) for (int j = 0; j <= cnt; j ++) g[i][j] = 0; g[0][0] = INF; for (int i = 0; i < trans[p][x].size(); i ++) for (int j = 0; j <= cnt; j ++) for (int k = 0; j + k <= cnt; k ++) g[i + 1][j + k] = max(g[i + 1][j + k], min(g[i][j], Dp(x, trans[p][x][i], k, tot))); return f[p][x][cnt][tot] = g[trans[p][x].size()][cnt] + val[p][x];}int main(){#ifdef wxh010910 freopen("data.in", "r", stdin);#endif mset(f, INF); n = Read(); for (int i = 1, x, y, w; i < n; i ++) x = Read(), y = Read(), w = Read(), adj[x].pb(y), adj[y].pb(x), val[x][y] = val[y][x] = w; for (int x = 1; x <= n; x ++) for (auto p : adj[x]) for (auto y : adj[x]) if (y ^ p) trans[p][x].pb(y); s = Read(), m = Read(); for (int i = 1; i <= m; i ++) siz[Read()] ++; Dfs(s, 0); int ret = INF; for (auto y : adj[s]) ret = min(ret, Dp(s, y, siz[y], m)); return printf("%d\n", ret), 0;}
阅读全文
0 0
- CodeForces 868 E. Policeman and a Tree
- [codeforces#438 E题]Policeman and a Tree
- Codeforces868E Policeman and a Tree -- DP
- Codeforces 463E Caisa and Tree(暴力)
- CodeForces 735E Ostap and Tree
- CodeForces 734E - Anton and Tree
- 【27.91%】【codeforces 734E】Anton and Tree
- codeforces 463E . Caisa and Tree
- Codeforces 763A-Timofey and a tree
- CodeForces 763A Timofey and a tree
- codeforces 739b Alyona and a tree
- CODEFORCES 739B Alyona and a tree
- 【codeforces 764C】Timofey and a tree
- CodeForces 764C Timofey and a tree
- codeforces 519E A and B and Lecture Rooms
- Codeforces 519E A and B and Lecture Rooms LCA
- CodeForces 519E A and B and Lecture Rooms
- codeforces E. A and B and Lecture Rooms
- Python Unicode码无法转为中文字符问题
- python mutilprocess
- QNX的license-key,试用qnx入门参考-QNX的密钥账号密码
- [杂题 贪心] Codeforces 808E. Selling Souvenirs
- APNS(Apple Push Notification Service)远程推送原理解析
- CodeForces 868 E. Policeman and a Tree
- request中文乱码/请求转发mvc/地址写法/防盗链
- 前端学科笔记分享——vue.js
- 高质量 Android 开发框架 LoonAndroid 详解
- 文件属性查看、所有人所有组管理,普通权限,特殊权限,访问控制,默认权限
- SCA 2017移动安全技术商业模式论坛——移动终端与物联网应用安全
- Caffe_Windows学习笔记(六)图形化操作工具digits的安装与运行
- leetcode Merge Two Sorted Lists
- 第四周第一课--栈