<span style="font-family: Arial, Helvetica, Verdana, sans-serif; background-color: rgb(255, 255, 255);">Treasure Hunt I</span>
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.
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 ith 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.
Just output the max value CC can get, and you should keep CC alive after m days.
2 1 3 1 2 1 1 2 2 1 3 2 1 1 2 1 2 3 3 1 2 1 2 5
4 3 6
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.
题目大意
很好懂,求一个树上的回路,要求代价和不超过m,并使得经过的点的收益达到最大。
思路
刚开始看样例以为是只有一个点,于是dfs枚举了每个终点……
后来发现都对于同一点可以经过两次以上
如
4
1 2 4 8
1 2 1
1 3 1
1 4 1
1 6
答案就是15
而将以上样例改为 k=1,m=5答案就会是13
就成了一个典型的树上的DP
dp[i][j]表示单程j天回到i点的最大收益
直接转移便是其所有孩子的一个背包问题了
卡了两下,注意边界。
/** **author : ahm001 ** **source : ZOJ3626 ** **time: 14/08/2014 ** **type DP/Tree ** **/ #include <cstdio> #include <iostream> #include <algorithm> #include <ctime> #include <cctype> #include <cmath> #include <string> #include <cstring> #include <stack> #include <queue> #include <list> #include <vector> #include <map> #include <set> #define sqr(x) ((x)*(x)) #define LL long long #define INF 0x3f3f3f3f #define PI acos(-1.0) #define eps 1e-10 using namespace std; int k,m; int ma[123][123]; int dp[123][2000]; int dis[123]; int v[123],n; void dfs(int x) { for (int i=0;i<=m/2;i++) dp[x][i]=v[x]; // printf("%d\n",vv); for (int i=1;i<=n;i++) if (dis[i]>dis[x]+ma[x][i]) { dis[i]=dis[x]+ma[x][i]; dfs(i); for (int j=m/2;j>=ma[x][i];j--) for (int k=0;k<=j-ma[x][i];k++)//注意处理的边界 { dp[x][j]=max(dp[x][j],dp[x][j-k-ma[x][i]]+dp[i][k]); } } } int main() { while (~scanf("%d",&n)) { memset(dp,0,sizeof dp); for (int i=1;i<=n;i++) scanf("%d",&v[i]); memset(ma,0x3f,sizeof ma); memset(dis,0x3f,sizeof dis); int o,p,q; for (int i=1;i<=n-1;i++) { scanf("%d%d%d",&o,&p,&q); ma[o][p]=ma[p][o]=q; } int ans=0; scanf("%d%d",&k,&m); dis[k]=0; dfs(k); for (int i=0;i<=m/2;i++) ans=max(ans,dp[k][i]); printf("%d\n",ans); } return 0; }
[zoj 3626]Treasure Hunt I 树DP,布布扣,bubuko.com
原文:http://blog.csdn.net/ahm001/article/details/38554435