ZOJ3626---Treasure Hunt I

Treasure Hunt I


Time Limit: 2 Seconds     
Memory Limit: 65536 KB



Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn‘t at his hometown. One day, a brave person named CC finds a treasure map, and he wants to get as much as possible.

Akiba consists of n towns and n-1 roads. There is a way from each town to any other. Each town contains some treasure values
Vi. CC starts from town k(his hometown), at day 0. After
m days, the bloodsucker will appear and CC would be killed if he hasn‘t been back yet, it means CC has
m days for hunting the treasure at most. It takes CC Ti days to move from one town to another neighbour town.(Two towns called neighbour if they are the endpoint of one road.) You can assume CC will get the treasure immediately
as he arrives at that town. CC wants to obtain as much value as possible, keeping him alive at the same time.

Input

There are multiple cases, about 50 cases.

The first line of each case contains an integer n, indicating there are
n
towns.

The following line describe the treasure‘s value in each town. "V1 V2 ...
Vn". Vi is the value of the treasure in
i
th town. Each value is separated by one blank.

The next n-1 lines describe the n-1 roads in Akiba. "i j Ti" Means the
ith town and the jth town are endpoints of that road. It takes
Ti
days to get through this road.

The last line has two integer k and m as described above.

1<=n<=100, 0<=Vi<=1000 , 1<=Ti<=10

1<=k<=n, 1<=m<=200

All the inputs are integers.

Output

Just output the max value CC can get, and you should keep CC alive after m days.

Sample Input

2
1 3
1 2 1
1 2
2
1 3
2 1 1
2 1
2
3 3
1 2 1
2 5

Sample Output

4
3
6

Hint

Sample 1: CC can go to town 2 and return at day 2.

Sample 2: CC can‘t come back within 1 day. So he can only take the treasure in his hometown.

Sample 3: CC only need 2 days to collect all the treasure.

树形dp, 设dp[u][i]表示用i天时间去访问以u为根的子树并且最后回到u,能够得到的最多的宝藏

dp[u][i] = max(dp[v][ j - cost[u, v] ] + dp[u][i - j] + val[v]);

/*************************************************************************
    > File Name: zoj3626.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年01月02日 星期五 14时34分00秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int dp[110][220];
int val[110], head[110];
int n, tot, m, k;

struct node
{
	int weight;
	int next;
	int to;
}edge[220];

void addedge(int from, int to, int weight)
{
	edge[tot].weight = weight;
	edge[tot].to = to;
	edge[tot].next = head[from];
	head[from] = tot++;
}

void dfs(int u, int fa)
{
	for (int i = head[u]; ~i; i = edge[i].next)
	{
		int v = edge[i].to;
		if (v == fa)
		{
			continue;
		}
		dfs(v, u);
		for (int j = m; j >= 0; --j)
		{
			for (int k = 2 * edge[i].weight; k <= j; ++k)
			{
				dp[u][j] = max(dp[u][j], dp[v][k - 2 * edge[i].weight] + dp[u][j - k] + val[v]);
			}
		}
	}
}

int main()
{
	int u, v, w;
	while (~scanf("%d", &n))
	{
		tot = 0;
		memset (head, -1, sizeof(head));
		memset (dp, 0, sizeof(dp));
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d", &val[i]);
		}
		for (int i = 1; i <= n - 1; ++i)
		{
			scanf("%d%d%d", &u, &v, &w);
			addedge(u, v, w);
			addedge(v, u, w);
		}
		scanf("%d%d", &k, &m);
		dfs(k, -1);
		printf("%d\n", dp[k][m] + val[k]);
	}
	return 0;
}

时间: 2024-10-13 22:43:29

ZOJ3626---Treasure Hunt I的相关文章

ZOJ-3626 Treasure Hunt I 树形DP

很久很久以前有一个吸血鬼,每m天就会出现一次,把不在自己村子呆着的冒险家吃掉.有n个村子,n-1条道路,每个村子都有一定数量的财富,默认探险家刚一到达一个村子就能获得财富,给出探险家的出生的村子和多少天后吸血鬼出现. 要求在吸血鬼出现之前赶回自己的村子,其实就是求一棵以k为根的各点权值之和最大且各边权加起来<=m/2的子树. #include <iostream> #include <cstdio> #include <cmath> #include <cs

【树形dp】Treasure Hunt I

[ZOJ3626]Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn't at his hometown. One day, a brave person named

[zoj 3626]Treasure Hunt I 树DP

<span style="font-family: Arial, Helvetica, Verdana, sans-serif; background-color: rgb(255, 255, 255);">Treasure Hunt I</span> Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since a bloodsucker living

POJ1066 Treasure Hunt(线段相交)

题目链接: http://poj.org/problem?id=1066 题目描述: Treasure Hunt Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-art technology they are able to determine

zoj Treasure Hunt IV

Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland, suddenly she fell into a hole, when she woke up, she found there are b - a + 1 treasures labled a from b in front of her.Alice was very excited but

ZOJ 3626 Treasure Hunt I(树形dp)

Treasure Hunt I Time Limit: 2 Seconds      Memory Limit: 65536 KB Akiba is a dangerous country since a bloodsucker living there. Sometimes the bloodsucker will appear and kill everyone who isn't at his hometown. One day, a brave person named CC finds

ZOJ3629 Treasure Hunt IV(找规律,推公式)

Treasure Hunt IV Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is exploring the wonderland, suddenly she fell into a hole, when she woke up, she found there are b - a + 1 treasures labled a from b in front of her. Alice was very excited but

POJ 1066 Treasure Hunt(计算几何)

Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5857   Accepted: 2439 Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-ar

zoj 3626 Treasure Hunt I (树形dp)

题目大意: 给出一棵树,求出从起点开始走m长度最后回到起点,所能得到的宝藏的最大价值. 思路分析: 通过一次dfs可以得到的是子树到根节点的所有距离的最大值. 现在的问题就是他走完一颗子树可以去另外一颗子树. 所以在回溯到根的时候要统计其他子树上互补距离的最大值. dp[i] [j] 表示i为根节点,在i的子树中走j步然后回到i所能拿到的最大价值. 转移方程就是 dp[x][i+2*len]=max(dp[x][i+2*len],dp[v][j]+dp[x][i-j]); v为x的子树的根,le

POJ 1066 Treasure Hunt(线段相交&amp;&amp;转换)

Treasure Hunt 大意:在一个矩形区域内,有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越只能在中点穿越. 思路:需要巧妙的转换一下这个问题,因为从一个点到终点不可能"绕过"围墙,只能穿过去,所以门是否开在中点是无所谓的,只要求四周线段中点到终点的线段与墙的最少交点个数即可.更进一步,实际上,只需判断四周围墙的所有点与终点的连线与内墙的最少交点加一即可. struct Point{ double x, y; } A,