ZOJ 4772 Treasure Hunt I 树形DP(背包) && hdu The Ghost Blows Light 树形DP(背包)
2012-09-16 10:36
525 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4772
题意:
给定n个点n-1条边,每个点对应一个财富值,走每条路径都对应着一个所需要的时间,问在m天内从k出发然后回到k,所能取得到的最大财富值 v1 V2 ....V1.
思路:
由于题目给定的是一棵生成树,所以从k出发后必须按原路返回才可满足条件,也就是每条边走两次。
dp[k][m]表示从k出发的又回到k的路径所取得的最大值。就是相当于往容量为m的包里放物品一样。每个点对应一个包。
View Code
题意:
给定n个点n-1条边,每个点对应一个财富值,走每条路径都对应着一个所需要的时间,问在m天内从k出发然后回到k,所能取得到的最大财富值 v1 V2 ....V1.
思路:
由于题目给定的是一棵生成树,所以从k出发后必须按原路返回才可满足条件,也就是每条边走两次。
dp[k][m]表示从k出发的又回到k的路径所取得的最大值。就是相当于往容量为m的包里放物品一样。每个点对应一个包。
View Code
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string> #define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a,b) (a) > (b)? (b):(a) #define Max(a,b) (a) > (b)? (a):(b) #define ll __int64 #define inf 0x7f7f7f7f #define MOD 100000007 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 507 #define N 107 using namespace std; //freopen("din.txt","r",stdin); struct node{ int v,w; int next; }g ; int head ,ct; int dis ,dp [M],val ; int pre ,path ; bool vt ; int n,T; void add(int u,int v,int w){ g[ct].v = v; g[ct].w = w; g[ct].next = head[u]; head[u] = ct++; } void spfa(){ queue<int>q; int i; CL(vt,false); CL(pre,-1); for (i = 1; i <= n; ++i) dis[i] = inf; dis[1] = 0; vt[1] = true; q.push(1); while (!q.empty()){ int u = q.front(); q.pop(); for (i = head[u]; i != -1; i = g[i].next){ int v = g[i].v; int w = g[i].w; if (dis[v] > dis[u] + w){ pre[v] = u;//记录父节点 path[v] = i;//记录路径编号 dis[v] = dis[u] + w; if (!vt[v]){ vt[v] = true; q.push(v); } } } vt[u] = false; } for (i = n; i != 1; i = pre[i]){//置0 g[path[i]].w = 0; } } void dfs(int u){ vt[u] = true; for (int i = head[u]; i != -1; i = g[i].next){ int v = g[i].v; int w = g[i].w*2; if (vt[v]) continue; dfs(v); for (int k = T; k >= w; --k){ for (int j = 0; j <= k - w; ++j) dp[u][k] = max(dp[u][k],dp[u][k - w - j] + dp[v][j]);// 这里当w为0时保证能够选入 } } for (int i = 0; i <= T; ++i) dp[u][i] += val[u]; } int main(){ int i; int u,v,w; while (~scanf("%d%d",&n,&T)){ ct = 0; CL(head,-1); for (i = 2; i <= n; ++i){ scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } for (i = 1; i <= n; ++i) scanf("%d",&val[i]); spfa(); //spfa求最短路后不满足条件 if (dis > T){ printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n"); continue; } T -= dis ; CL(vt,false); CL(dp,0); dfs(1); printf("%d\n",dp[1][T]); } return 0; }
相关文章推荐
- 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)
- HDU 4276 The Ghost Blows Light [树形背包DP]
- hdu 4276 The Ghost Blows Light(树形DP+最短路+分组背包)好题。。。
- HDU 4276 The Ghost Blows Light(12年长春 树形DP+spfa)
- hdu 4276 The Ghost Blows Light - 树形dp
- **HDU 4276 - The Ghost Blows Light(树形DP)
- hdu 4276 The Ghost Blows Light(Spfa+树形dp)
- HDU-4276 The Ghost Blows Light (树形DP+背包)
- hdu 4276 The Ghost Blows Light (树形dp)
- hdu 4276 树形背包 The Ghost Blows Light
- *(中等) 树形dp+分组背包 HDU 4276 The Ghost Blows Light
- hdu 4276 The Ghost Blows Light(DP-树形DP)
- HDU 4276 树形DP The Ghost Blows Light
- HDU 4276 The Ghost Blows Light【树形DP】
- HDU 4276 The Ghost Blows Light(树形DP)
- 【树形DP】【HDU 4276】The Ghost Blows Light
- HDU 4276 The Ghost Blows Light (树形DP)
- HDU 4276 The Ghost Blows Light 树形dp
- HDOJ 4276 The Ghost Blows Light(树形DP+背包)
- SPFA + 树形DP:The Ghost Blows Light