Meaning
An N-node tree with a positive integer that represents the distance between two nodes. Your task is to answer this query: From the node, go no more than X unit distance,
How many nodes can be passed at most? The same node passes many times and can only be counted as one.
Ideas
This problem is also seen many days ago, in today's solution. Dynamic planning is so interesting:)
Traversing n nodes, there are two cases, the first is not back to the starting point after the traversal, the second is to return to the starting point.
Both may repeat over some edges, but obviously the second traversal will cost more
In this question, you do not need to return to the starting point after the traversal.
F (i, J, 0): Represents the J node traversing the subtree I, without going back to the minimum cost of the I point
F (I, J, 1): Represents the J-node traversing the subtree I, and eventually back to the minimum cost of the I point
F (I, j, 1) = min{min{f (i, j-k, 1) + f (V, K, 1) + 2*w | 1<=k<j} | v is son of I node}
F (i, j, 0) = min{min{min{f (i,j-k,1) +f (v,k,0) +w, F (i,j-k,0) +f (v,k,1) +2*w} | 1<=k<j} | v is son of I node}
Code
/**===================================================== * This are a solution for ACM/ICPC problem * * @source: uva-1407
Caves * @description: Tree-Type Backpack DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com
* Copyright (C) 2013/08/23 14:06 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> # Include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring
> #define MP make_pair using namespace std;
typedef pair<int, int> PII;
typedef long long Int64;
const int INF = 0X3F3F3F3F;
const int MAXN = 510;
Vector<pii >adj[MAXN];
int n, q;
int TOT[MAXN];
int f[maxn][maxn][2];
BOOL VIS[MAXN];
int dfs (int u) {Vis[u] = true;
Tot[u] = 1; Count Child tree nodes for (int i = 0; i < adj[u].size (); ++i) {int v = adj[u][i].first; int w = Adj[u][i].second; if (VIS[V)) CO
Ntinue;
Tot[u] + = DFS (v); VIS[V]= false;
}//dp//www.bianceng.cn f[u][1][0] = f[u][1][1] = 0; for (int i = 0; i < adj[u].size (); ++i) {int v = adj[u][i].first; int w = Adj[u][i].second; if (vis[v)) continue; (int s = tot[u]; s >= 1;--s) {for (int j = 1; J <= Tot[v] && J < s; ++j) {int tmp1 = f[u][s-j][1] + f[v][j][0] + w; int tmp2 = F[u][s-j
][0] + f[v][j][1] + 2 * w;
F[u][s][0] = min (f[u][s][0], min (TMP1, TMP2));
F[u][s][1] = min (f[u][s][1], f[u][s-j][1] + f[v][j][1] + 2 * w);
}} return Tot[u]; int main () {int cas = 1 while (~scanf ("%d", &n) && N) {//init for (int i = 0; I <= N; +
+i) adj[i].clear ();
for (int i = 0; i < n-1 ++i) {int u, V, W; scanf ("%d%d%d", &v, &u, &w); Adj[u].push_back (MP (V, W));
memset (Vis, 0, sizeof (VIS));
Memset (f, INF, sizeof (f));
DFS (0);
scanf ("%d", &q);
printf ("Case%d:\n", cas++); while (q--) {int D; scanf ('%d ', &d); for (int i = n; I >= 1;-i) {if (f[0][i][0] <= d) {
printf ("%d\n", I);
Break
return 0; }