ZOJ 3626 Treasure Hunt I / 树形DP
来源:互联网 发布:java手机色游戏破解版 编辑:程序博客网 时间:2024/05/25 05:37
也可以叫做树形背包 因为在回溯的时候像01背包一样 枚举每个物品 从最大容量开始更新
这里的物品代表以i为根的子树
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 110;struct node{int e;int w;};int dp[maxn][maxn*2];vector <node> a[maxn];int b[maxn];bool vis[maxn];int n, s, m;void dfs(int u, int t){//if(vis[u][t])//return dp[u][t];if(t < 0)return;for(int i = 0; i <= t; i++)dp[u][i] = b[u];vis[u] = true;for(int i = 0; i < a[u].size(); i++){node v = a[u][i];if(vis[v.e])continue;dfs(v.e, t-2*v.w);for(int j = t; j >= 2*v.w; j--){for(int k = 0; k <= j-2*v.w; k++){node vv = a[v.e][i];dp[u][j] = max(dp[u][j], dp[u][j-k-2*v.w] + dp[v.e][k]);}}}}int main(){while(scanf("%d", &n) == 1){for(int i = 1; i <= n; i++){scanf("%d", &b[i]);a[i].clear();}for(int i = 1; i < n; i++){int u, v, w;scanf("%d %d %d", &u, &v, &w);a[u].push_back((node){v, w});a[v].push_back((node){u, w});}scanf("%d %d", &s, &m);memset(dp, 0, sizeof(dp));memset(vis, false, sizeof(vis));dfs(s, m);printf("%d\n", dp[s][m]);}return 0;}
0 0
- ZOJ 3626 Treasure Hunt I(树形DP)
- ZOJ 3626 Treasure Hunt I / 树形DP
- Treasure Hunt I - ZOJ 3626 树形dp
- ZOJ-3626 Treasure Hunt I 树形DP
- zoj-3626 Treasure Hunt I (树形dp)
- zoj 3626 Treasure Hunt I(树形DP+分组背包)
- zoj 3626 Treasure Hunt I (树形dp)
- ZOJ 3626 Treasure Hunt I(树形dp)
- ZOJ 3626 Treasure Hunt I (树形dp)
- ZOJ 3626 Treasure Hunt I 树形背包
- zoj 3626 Treasure Hunt I(树形背包)
- [zoj 3626]Treasure Hunt I 树DP
- ZOJ 3626 —— Treasure Hunt I(树形DP + 背包)
- zoj 3626 Treasure Hunt I
- ZOJ 3626 Treasure Hunt I
- ZOJ 3626 Treasure Hunt I
- ZOJ 3626 Treasure Hunt I(树型DP+01背包)
- zoj 3626 Treasure Hunt I(树背包)
- Mybatis使用多个参数查询
- NYOJ 20 吝啬的国度 (搜索)
- Hadoop专业解决方案-第12章 为Hadoop应用构建企业级的安全解决方案
- 寒假训练---训练赛2--Fighting
- a和&a的区别
- ZOJ 3626 Treasure Hunt I / 树形DP
- 最小生成树(kruskal算法)
- 学习Spring4.0——一种新的架构风格和潮流
- 【OpenCV学习笔记】【函数学习】三(cvGetCaptureProperty函数)
- 你必须了解的Session的本质
- 解决IllegalStateException: Can not perform this action after onSaveInstanceState
- 【备忘】一个标准GDI窗口框架
- 计算波特率
- JAVA入门-----1