Algorithm: UVA 1407 Caves (tree-type backpack DP)

Source: Internet
Author: User

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 &lt;iostream&gt; #include &lt;cstdio&gt; # Include &lt;algorithm&gt; #include &lt;vector&gt; #include &lt;queue&gt; #include &lt;cmath&gt; #include &lt;cstring
    
&gt; #define MP make_pair using namespace std;
typedef pair&lt;int, int&gt; PII;
typedef long long Int64;
    
const int INF = 0X3F3F3F3F;
    
const int MAXN = 510;
Vector&lt;pii &gt;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 &lt; 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 &lt; 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 &gt;= 1;--s) {for (int j = 1; J &lt;= Tot[v] &amp;&amp; J &lt; 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", &amp;n) &amp;&amp; N) {//init for (int i = 0; I &lt;= N; +
    
+i) adj[i].clear ();
    
for (int i = 0; i &lt; n-1 ++i) {int u, V, W; scanf ("%d%d%d", &amp;v, &amp;u, &amp;w); Adj[u].push_back (MP (V, W));
memset (Vis, 0, sizeof (VIS));
Memset (f, INF, sizeof (f));
    
DFS (0);
scanf ("%d", &amp;q);
printf ("Case%d:\n", cas++); while (q--) {int D; scanf ('%d ', &amp;d); for (int i = n; I &gt;= 1;-i) {if (f[0][i][0] &lt;= d) {
printf ("%d\n", I);
Break
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.