题意:给一棵树,一个人站在节点s,他有m天时间去获取各个节点上的权值,并且最后需要回到起点s,经过每条边需要消耗v天,问最少能收获多少权值?

思路:  常规的,注意还得跑回原地s。

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=;
int w[N], head[N], dp[N][N], edge_cnt, n;
struct node
{
int from,to,cost,next;
node(){};
node(int from,int to,int cost,int next):from(from),to(to),cost(cost),next(next){};
}edge[N*]; void add_node(int from,int to,int cost)
{
edge[edge_cnt]=node(from,to,cost,head[from]);
head[from]=edge_cnt++;
} void DFS(int t,int far,int m)
{
for(int j=m; j>=; j--) dp[t][j]=w[t]; //本节点的权值
node e;
for(int i=head[t]; i!=-; i=e.next)
{
e=edge[i];
if(e.to!=far && m-*e.cost>=)
{
DFS(e.to, t, m-*e.cost);
for(int j=m; j>; j--)
for(int k=*e.cost; k<=j; k++) //给孩子k-2*cost天
dp[t][j]=max(dp[t][j], dp[t][j-k]+dp[e.to][k-*e.cost]);
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
int a, b, c, s, m;
while(~scanf("%d",&n))
{
memset(head, -, sizeof(head));
memset(dp, , sizeof(dp));
edge_cnt=; for(int i=; i<=n; i++) scanf("%d",&w[i]);
for(int i=; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
add_node(a,b,c);
add_node(b,a,c);
}
scanf("%d%d",&s,&m);
DFS(s,-,m);
printf("%d\n",dp[s][m]);
}
return ;
}

AC代码

ZOJ 3626 Treasure Hunt I (树形DP,常规)的更多相关文章

  1. ZOJ 3626 Treasure Hunt I 树上DP

    E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...

  2. ZOJ 3626 Treasure Hunt I(树形dp)

    Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since ...

  3. zoj 3629 Treasure Hunt IV 打表找规律

    H - Treasure Hunt IV Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  4. zoj 3627 Treasure Hunt II (贪心)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:zoj-3627 题意 直线上有n个城市, 第i个城市和i+1个城市是相邻的.  每个城市都有vi的金币.   ...

  5. ZOJ 3627 Treasure Hunt II (贪心,模拟)

    题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集 ...

  6. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

  7. 【DP_树形DP专题】题单总结

    转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...

  8. ZOJ 3626(树形DP+背包+边cost)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...

  9. 【树形dp】Treasure Hunt I

    [ZOJ3626]Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous coun ...

随机推荐

  1. sublimelinter-php 错误代码提示

    先安装 SublimeLinter 如同其他插件一样使用 Package Control 来安装. 按下 Ctrl+Shift+p 进入 Command Palette 输入install进入 Pac ...

  2. js 拦截 窗体关闭事件

    <script type="text/javascript">   <!--         window.onbeforeunload = onbeforeun ...

  3. 【220】◀▶ IDL 数组操作函数说明

    参考:Array Creation Routines —— 创建数组函数参考:Array Manipulation Routines —— 操作数组函数 01   MAX 最大值. 02   MIN ...

  4. 3.17-3.18 HDFS2.x中高级特性讲解

    一.hdfs federation hdfs federation即hdfs的联邦:可以理解为有多个namenode节点的hdfs集群: HA方案解决的是单点故障问题,而Fdederation解决的是 ...

  5. AIRSDK 3.7 加载远程的含有代码的swf文件

    之前就说这个版本会解决可以加载远程的含有代码的swf文件的需求.但是,一直比较好奇这个是否行得通,还以为 Adobe 副总裁去了苹果,内部给了特殊待遇. 因为苹果一直就是不允许远程加载代码的,像js文 ...

  6. Tomcat注入到系统服务中,实现服务自启动及常驻

    步骤一.打开service.bat 并修改(推荐notepad++):  1.set SERVICE_NAME=Tomcat6(服务名称,可在命令行中通过该名字进行服务的控制(启动/关闭)) 2.se ...

  7. WindowsService服务程序开发 安装和卸载

    安装服务:installutil.exe E:\XTestDemo\X_15_WindowsService\bin\Debug\X_15_WindowsService.exe 卸载服务:install ...

  8. HDU3038【种类并查集】

    题意: 给出m组区间[a,b],以及其区间的和,问有矛盾的有几组: 思路: 种类并查集. 主要是几个关系:同类元素的关系,父亲与儿子的关系,不同类元素的关系: 我们可以类似看作一个前缀和,sum[x] ...

  9. hoj2188 WordStack

    WordStack My Tags   (Edit)   Source : Mid-Atlantic 2005   Time limit : 5 sec   Memory limit : 32 M S ...

  10. PJzhang: github与出口管制·说明条款

    猫宁!!! 参考链接:https://www.infoq.cn/article/KMl2EO*PSMxIkVREiYvC https://help.github.com/en/articles/git ...