题意:给一棵树,一个人站在节点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. struts2 ValueStack的set方法与setValue方法的区别

    struts2中 ValueStack的set方法与setValue方法的区别呢? 示例代码: ActionContext.getContext().getValueStack().setValue( ...

  2. java web 工程找不到tomcat类 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    ava.lang.ClassNotFoundException: com.mysql.jdbc.Driverat org.apache.catalina.loader.WebappClassLoade ...

  3. insert插入错误

    16:24:30,803 Fetching JDBC Connection from DataSource 16:24:30,826 Returning JDBC Connection to Data ...

  4. C#字典常用技巧

    说明    必须包含名空间System.Collection.Generic     Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)     键必须是唯一的,而值不需要唯 ...

  5. 关于window 查看端口命令

    今天小R又知道了一个新的小常识,个人觉得还是蛮实用的,可能对于有些新手来讲应该也不知道吧. 如:大家都知道window查看命令是用  “netstat -an”   , 如图显示一大推打开的端口. 但 ...

  6. 深入理解Java中方法的参数传递机制

    形参和实参 我们知道,在Java中定义方法时,是可以定义参数的,比如: public static void main(String[] args){ } 这里的args就是一个字符串数组类型的参数. ...

  7. Codevs 1569 最佳绿草

    1569 最佳绿草  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 贝茜正计划着这一天如何美美地咀嚼春天的绿 ...

  8. [Xcode 实际操作]八、网络与多线程-(6)使用UIApplication对象打开地图

    目录:[Swift]Xcode实际操作 本文将演示如何使用应用程序单例对象,打开地图的功能. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKi ...

  9. 爬虫之图片懒加载技术、selenium和PhantomJS

    爬虫之图片懒加载技术.selenium和PhantomJS   图片懒加载 selenium phantomJs 谷歌无头浏览器 一.图片懒加载 什么是图片懒加载? 案例分析:抓取站长素材http:/ ...

  10. Exception in thread "main" java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package解决办法(图文详解)

    不多说,直接上干货! 问题详情 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF ...