hdu 4276 树形背包
来源:互联网 发布:linux 开机启动 编辑:程序博客网 时间:2024/05/22 04:27
The Ghost Blows Light
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1909 Accepted Submission(s): 585
Problem Description
My name is Hu Bayi, robing an ancient tomb in Tibet. The tomb consists of N rooms (numbered from 1 to N) which are connected by some roads (pass each road should cost some time). There is exactly one route between any two rooms, and each room contains some treasures. Now I am located at the 1st room and the exit is located at the Nth room.
Suddenly, alert occurred! The tomb will topple down in T minutes, and I should reach exit room in T minutes. Human beings die in pursuit of wealth, and birds die in pursuit of food! Although it is life-threatening time, I also want to get treasure out as much as possible. Now I wonder the maximum number of treasures I can take out in T minutes.
Input
There are multiple test cases.
The first line contains two integer N and T. (1 <= n <= 100, 0 <= T <= 500)
Each of the next N - 1 lines contains three integers a, b, and t indicating there is a road between a and b which costs t minutes. (1<=a<=n, 1<=b<=n, a!=b, 0 <= t <= 100)
The last line contains N integers, which Ai indicating the number of treasure in the ith room. (0 <= Ai <= 100)
The first line contains two integer N and T. (1 <= n <= 100, 0 <= T <= 500)
Each of the next N - 1 lines contains three integers a, b, and t indicating there is a road between a and b which costs t minutes. (1<=a<=n, 1<=b<=n, a!=b, 0 <= t <= 100)
The last line contains N integers, which Ai indicating the number of treasure in the ith room. (0 <= Ai <= 100)
Output
For each test case, output an integer indicating the maximum number of treasures I can take out in T minutes; if I cannot get out of the tomb, please output "Human beings die in pursuit of wealth, and birds die in pursuit of food!".
Sample Input
5 101 2 2 2 3 22 5 33 4 31 2 3 4 5
Sample Output
11
给定一棵n个节点的树,每个节点都有权值,从1点出发,在时间T内到达n,且获得最大的权值和。
由于是树,每两个点之间都有唯一的路径,从1到n之间的必经之路唯一,因此那总时间减去这条路上的权值和,这条路上的边权全部清零,就相当于缩点,
每条边要么访问两次,要么不访问,然后进行一次树形背包得到答案。
代码:
/* ***********************************************Author :xianxingwuguanCreated Time :2014-2-6 22:31:52File Name :1.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const int maxn=200;int head[maxn],tol,dp[maxn][5*maxn],weight[maxn],T,t,n;struct node{ int next,to,val; node(){}; node(int _next,int _to,int _val):next(_next),to(_to),val(_val){} }edge[5*maxn]; void add(int u,int v,int val){ edge[tol]=node(head[u],v,val); head[u]=tol++; } bool dfs(int u,int pre){ if(u==n)return 1;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(v==pre)continue;if(dfs(v,u)){t+=edge[i].val;edge[i].val=0;return 1;}}return 0;}void dfs1(int u,int fa){ for(int i=0;i<=T;i++)dp[u][i]=weight[u]; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(v==fa)continue; dfs1(v,u); int pp=2*edge[i].val; for(int j=T;j>=pp;j--) for(int k=0;k<=j-pp;k++) dp[u][j]=max(dp[u][j],dp[v][k]+dp[u][j-pp-k]); }}int main(){ //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int i,j,k,p; while(~scanf("%d%d",&n,&T)){ memset(head,-1,sizeof(head));tol=0; for(i=1;i<n;i++){ scanf("%d%d%d",&j,&k,&p); add(j,k,p); add(k,j,p); } for(i=1;i<=n;i++)scanf("%d",&weight[i]); t=0; dfs(1,1); if(t>T){ puts("Human beings die in pursuit of wealth, and birds die in pursuit of food!"); continue; } memset(dp,0,sizeof(dp)); T-=t; dfs1(1,-1); cout<<dp[1][T]<<endl; } return 0;}
0 0
- hdu 4276 树形背包
- hdu 4276 树形dp背包
- HDU 4276 树形dp + 背包
- hdu 4276 树形DP + 分组背包
- HDU 4276 树形dp+spfa+分组背包
- hdu 1011 树形背包
- hdu 1011 树形背包
- hdu 1054 树形背包
- hdu 1011 树形dp 背包
- hdu 1011 简单树形背包。
- hdu 5148 Cities(树形背包)
- HDU 1561 树形dp+背包
- HDU 4003 树形DP+背包
- hdu 4276 树形背包 The Ghost Blows Light
- hdu 1561树形dp+分组背包
- hdu 3593 树形依赖背包的优化
- hdu 4044 GeoDefense 树形dp+分组背包
- HDU 1011 Starship Troopers (树形DP+背包)
- ZOJ 1489 2^x mod n = 1 数论
- Target runtime Apache Tomcat v6.0 is not defined.错误解决方法
- [母函数]HDU 1709 The Balance
- Dll注入技术之消息钩子
- UVa 156 反语片
- hdu 4276 树形背包
- 85. 两个头脑往往比一个更好
- Dll注入技术之远程线程注入
- usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)
- Oracle 树形SQL语句,SYS_CONNECT_BY_PATH 函数
- Dll注入技术之依赖可信进程注入
- 86. 两个错误的结果可以是正确(也会更难修复)
- 寻找发帖“水王”
- Android 网络调试