ZOJ 3626 Treasure Hunt I

Description

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. "V1V2 ... Vn". Vi is the value of the treasure in ith town. Each value is separated by one blank.
The next n-1 lines describe the n-1 roads in Akiba. "ijTi" 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+01揹包模型

第一次寫樹形DP,寫得好慌。有些的地方還是弄得模模糊糊……

dp[i][j]表示在第j天訪問節點i所能拿到的最多分數。

因爲是在樹上跑dp,所以每個節點最多訪問一次,每次只可能訪問這個節點兒子(就是dfs的過程)

#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

struct node
{int v,t;};
int dp[110][110],N,K,M;
bool vis[110];
vector<node> Chain[110];

inline int max(int a,int b)
{return a>b?a:b;}
inline void updata(int &a,int b)
{a=max(a,b);}

void readdata()
{
	memset(dp,0,sizeof(dp));
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=N;i++)
	{
		//scanf("%d",&v[i]);
		scanf("%d",&dp[i][0]);
		Chain[i].clear();
	}
	for(int i=1;i<N;i++)
	{
		int a,b,c; scanf("%d%d%d",&a,&b,&c);
		Chain[a].push_back((node){b,c});
		Chain[b].push_back((node){a,c});
	}
	scanf("%d%d",&K,&M); M=M>>1;
}

void dfs(int u)
{
    vis[u]=1;
	//dp[u][0]=v[u];
	for(int i=0;i<Chain[u].size();i++)
	{
		int v=Chain[u][i].v,t=Chain[u][i].t;
		if(!vis[v])//保證每個點只用一次
		{
			dfs(v);
			//j枚舉揹包容量
			for(int j=M;j>=0;j--)//倒着循環保證每個物品用一次
                for(int k=0;k+t<=j;k++)//打算用多少天走到下一個點
                    updata(dp[u][j],dp[u][j-k-t]+dp[v][k]);
		}
	}
}

void print()
{
	int ans=-1;
	for(int i=0;i<=M;i++) updata(ans,dp[K][i]);
	printf("%d\n",ans);
}

int main()
{
    freopen("E.in","r",stdin);
	while(scanf("%d",&N)==1)
	{
		readdata();
		dfs(K);
		print();
	}
	while(1);
	return 0;
}


發佈了123 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章