ZOJ 4772 Treasure Hunt I 树形DP(背包) && hdu The Ghost Blows Light 树形DP(背包)
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
#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 207 #define N 107 using namespace std; //freopen("din.txt","r",stdin); struct node{ int v,w; int next; }g[N*2]; int head[N],ct,val[N]; bool vt[N]; int dp[N][M]; int m,n; 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 dfs(int u){ int i; vt[u] = true; for (i = head[u]; i != -1; i = g[i].next){ int v = g[i].v; int w = 2*g[i].w; if (vt[v]) continue; dfs(v); //关键是这里不好理解,相当于分组背包 for (int k = m; 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]); } } } for (i = 0; i <= m; ++i) dp[u][i] += val[u]; } int main(){ //freopen("din.txt","r",stdin); int i,k; int u,v,w; while (~scanf("%d",&n)){ ct = 0; CL(head,-1); for (i = 1; i <= n; ++i) scanf("%d",&val[i]); for (i = 2; i <= n; ++i){ scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } scanf("%d%d",&k,&m); CL(vt,false); CL(dp,0); dfs(k); printf("%d\n",dp[k][m]); } return 0; }
http://acm.hdu.edu.cn/showproblem.php?pid=4276
题意:
给定n个点n-1条边,每个点对应一个财富值,走每条路径都对应着一个所需要的时间,问在T天内从1出发到N做能取得的最大财富值。
思路:
这题目就是上边一题的加强版,我们首先同spfa求一条最短路先不管财富值得大小,弱最小距离满足条件即可找最大财富值了,最短路一定会走,我们通过不在最短路上的点来扩充财富值,那么不在最短路上的点要么不走要么走两次。这样dp就好了,和上边一样。
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[N]; int head[N],ct; int dis[N],dp[N][M],val[N]; int pre[N],path[N]; bool vt[N]; 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[n] > T){ printf("Human beings die in pursuit of wealth, and birds die in pursuit of food!\n"); continue; } T -= dis[n]; CL(vt,false); CL(dp,0); dfs(1); printf("%d\n",dp[1][T]); } return 0; }