[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 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 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.

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.


Author: LI, Chao
Contest: ZOJ Monthly, July 2012

试题分析:设dp[i][j]表示在i号节点走j步能获得的最大宝藏数。

     就像泛化背包那样,由于还要回去,所以得出转移方程:

     dp[i][j]=max(dp[i][j-t-2*Cost[x]]+dp[i->son][t],dp[x][j])

     为什么不是减去2*t呢,因为之前的已经在dp[i->son][t]中算过了。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,M,K;
int val[101],dp[101][201];
int Node[301],Root[301],Cost[301],Next[301];
int cnt; void addedge(int u,int v,int w){
cnt++;
Node[cnt]=v;
Cost[cnt]=w;
Next[cnt]=Root[u];
Root[u]=cnt;
return ;
}
void init(){
for(int i=1;i<=N;i++)
for(int j=0;j<=M;j++) dp[i][j]=val[i];
return ;
}
void dfs(int x,int fa){
for(int i=Root[x];i;i=Next[i]){
int son=Node[i];
if(son==fa) continue;
dfs(son,x);
}
for(int i=Root[x];i;i=Next[i]){
int son=Node[i];
if(son==fa) continue;
for(int j=M;j>=1;j--){
for(int t=0;t<=M;t++){
if(j<t+2*Cost[i]) break;
dp[x][j]=max(dp[x][j-t-2*Cost[i]]+dp[son][t],dp[x][j]);
}
}
}
return ;
} int main(){
while(scanf("%d",&N)!=EOF){
cnt=0;
memset(Next,0,sizeof(Next));
memset(Node,0,sizeof(Node));
memset(Root,0,sizeof(Root));
memset(Cost,0,sizeof(Cost));
for(int i=1;i<=N;i++) val[i]=read();
for(int i=1;i<N;i++){
int u=read(),v=read(),w=read();
addedge(u,v,w);
addedge(v,u,w);
}
K=read(),M=read();
init();
dfs(K,-1);
printf("%d\n",dp[K][M]);
}
}

最新文章

  1. [译]ZOOKEEPER RECIPES-Barriers
  2. 深入java集合学习1-集合框架浅析
  3. Linux LVM硬盘管理之二:创建逻辑卷步骤
  4. WCF服务客户端首页调用慢的问题处理
  5. tomcat(一)--java基础
  6. String 归档
  7. 一个漂亮的php验证码类(分享)
  8. 转:switch内部的变量定义问题(goto类似)
  9. 各种SQL类型
  10. c - 十六进制 转 十进制
  11. bootstrapvalidator之API学习
  12. BZOJ 1179 [Apio2009]Atm(强连通分量)
  13. java的引用数据类型,你知道吗???
  14. 周末班:Python基础之网络编程
  15. spring boot 自动配置原理
  16. 洛谷 P1016 旅行家的预算
  17. 爬虫--cheerio
  18. vue-cli、create-react-app 项目如何查看打包分析?
  19. 使用 urllib 解析 URL 链接
  20. 怎么清除Win10运行中的使用记录,不记录win10的运行记录

热门文章

  1. Windows Server 2008 R2 SP1安装SQL 2012安装报错之0x858C001B
  2. Python 开发中easy_install的安装及使用
  3. Coursera在线学习---第九节(1).异常数据检测(Anomaly Detection)
  4. 集合框架源码学习之ArrayList
  5. PHP序列化、反序列化常用的魔术方法
  6. python实战===itchat
  7. MACACA===gradle下载和安装
  8. python的时间和日期--time、datetime应用
  9. javascript 常用DOM操作整理
  10. php的设计模式