ZOJ 3626 Treasure Hunt I (树形DP,常规)
题意:给一棵树,一个人站在节点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,常规)的更多相关文章
- ZOJ 3626 Treasure Hunt I 树上DP
E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...
- ZOJ 3626 Treasure Hunt I(树形dp)
Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous country since ...
- zoj 3629 Treasure Hunt IV 打表找规律
H - Treasure Hunt IV Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- zoj 3627 Treasure Hunt II (贪心)
本文出自 http://blog.csdn.net/shuangde800 题目链接:zoj-3627 题意 直线上有n个城市, 第i个城市和i+1个城市是相邻的. 每个城市都有vi的金币. ...
- ZOJ 3627 Treasure Hunt II (贪心,模拟)
题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集 ...
- 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...
- 【DP_树形DP专题】题单总结
转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...
- ZOJ 3626(树形DP+背包+边cost)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...
- 【树形dp】Treasure Hunt I
[ZOJ3626]Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous coun ...
随机推荐
- java中有关socket通信的学习笔记
最近做的项目中使用到了一些基于java的socket长连接的一些功能,用来穿透有关行业的网闸.用到了也就学习了一下,下面是对学习内容的一个笔记,记录一下也希望有兴趣的同学可以参考一下,加深对javas ...
- Ladies' Shop
题意: 有 $n$ 个包,设计最少的物品体积(可重集),使得 1. 对于任意一个总体积不超过给定 $m$ 的物体集合有其体积和 恰好等于一个包的容量. 2.对于每一个包,存在一个物品集合能恰好装满它. ...
- C. Pearls in a Row
C. Pearls in a Row time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Asset Catalog Help (八)---Customizing Image Sets for Devices
Customizing Image Sets for Devices Add images to a set that are customized for display on the device ...
- MPTCP in Wireshark(转)
最新的wireshark可以直接识别出mptcp. Wireshark is a widely used network analyzer that can capture network traff ...
- lightoj 1074【spfa判负环】
题意: 给你一幅图,dis(u->v)的权值就是(w[v]-w[u])*(w[v]-w[u])*(w[v]-w[u]),所以有可能是负的,给你n个询问,给出最短路,长度<3或者不可达输出& ...
- SpringBoot2.0 整合 QuartJob ,实现定时器实时管理
一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...
- JIRA中的标记语言的语法参考
前言 看到网上有的文章说JIRA是使用Textile这门标记语言,有些语法和Wikitext和Markdown相像.JIRA在2017年进行了一次大更新,某些语法可能和以前不大一样,这里纪录一下常用的 ...
- CSS以及JS各种库的在线CDN引用地址
JS类—— html5.js,让你的IE浏览器支持H5新特性:http://html5shiv.googlecode.com/svn/trunk/html5.js (记得要注释判断哦) JQuer ...
- windows安装redis和PHP redis扩展
1.安装Redis (1)这里选择在github官网上下载Redis,地址:Redis下载地址 下载压缩包(如下图),并解压到本地目录,我放在D:\wamp\redis\redis-windows ( ...