题意:给一棵树,一个人站在节点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. Ubuntu 图形处理软件

    sudo add-apt-repository ppa:dhor/myway sudo apt-get update sudo apt-get install photivo

  2. HDU-2617

    Happy 2009 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. _beginThreadex的用法

    http://blog.csdn.net/cjcy1984001/article/details/6675669 线程开始和停止函数!  unsigned long _beginthreadex( v ...

  4. sql语句之数据类型

    整形类型:该类型没必要指定显示宽度,使用默认的就ok. 类型 大小 TINYINT 1字节 SMALLINT 2字节 MEDIUMINT 3字节 INT或INTEGER 4字节(后面加的宽度不是存储宽 ...

  5. Flutter实战视频-移动电商-49.详细页_Stack制作底部工具栏

    49.详细页_Stack制作底部工具栏 一直悬浮在最下面的 Stack层叠组件.里面用Row 可以横向布局 开始 stack如果想定位就要用position去定位. 修改return返回值的这个地方 ...

  6. MR 图像分割 相关论文摘要整理

    <多分辨率水平集算法的乳腺MR图像分割> 针对乳腺 MR 图像信息量大.灰度不均匀.边界模糊.难分割的特点, 提出一种多分辨率水平集乳腺 MR图像分割算法. 算法的核心是首先利用小波多尺度 ...

  7. 已有项目 iPhoneX 适配

    一.Assets 文件图片管理下的 LaunchImage 添加 iOS8.0 and latter 一项,并放一张 1125*2436 的LaunchPage 到对应的位置上. 二.有关 iPhon ...

  8. Weekly Contest 78-------->808. Soup Servings

    There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There a ...

  9. android摄像头获取图像——第二弹

    使用android内的Camera对象 (1)Camera是控制着摄像头的api,拥有一系列控制摄像头的上层方法:camera类能够调用底层的摄像头接口,完成启动摄像头.预 览摄像头图像.拍照等功能: ...

  10. FTP任务(重点看断点续传)

    一.FTP任务目录: 1. 多用户同时登陆:     socketserver 2. 用户登陆,加密认证: md5加密 3. 上传/下载文件,保证文件一致性:md5摘要 4. 传输过程中现实进度条 5 ...