Firetime limit:2000msmemory limit:65536kbthis problem'll be judged onPKU. Original id:2152
64-bit integer IO format: %lld Java class name: Main Country Z has N cities, which was numbered from 1 to N. Cities was connected by highways, and there was exact one path BETW Een different cities. Recently country Z often caught fire, so the government decided to build some firehouses in some cities. Build a firehouse in city K cost W (K). W for different cities is different. If there isn't firehouse in City K, the distance between it and the nearest city which have a firehouse, can ' t be more tha n D (K). D for different cities also is different. To save money, the government wants your to calculate the minimum cost to build firehouses.InputThe first line of input contains a single integer T representing the number of the test cases. The following T blocks each represents a test case.
The first line of each block contains an integer n (1 < N <= 1000). The second line contains N numbers separated to one or more blanks. The i-th number means W (i) (0 < W (i) <= 10000). The third line contains N numbers separated to one or more blanks. The i-th number means D (i) (0 <= D (i) <= 10000). The following N-1 lines each contains three integers u, V, L (1 <= u, v <= n,0 < L <=), which means there is a highway between City U and V of length L.
OutputFor each test case output, the minimum cost of a single line.Sample Input
551 1 1 1 11 1 1 1 11 2 12 3 13 4 14 5 151 1 1 1 12 1 1 1 21 2 12 3 13 4 14 5 151 1 3 1 12 1 1 1 21 2 12 3 13 4 14 5 142 1 1 13 4 3 21 2 31 3 31 4 244 1 1 13 4 3 21 2 31 3 31 4 2
Sample Output
21223
SourcePOJ Monthly,lou tiancheng Problem Solving: Lou Leader's topic is really difficult to refer to this Daniel's blog
$DP [I][j] Represents node I by J Fire, Best[i] indicates that each node of a subtree with the root of I takes a node within U as the minimum cost of taking charge of the station
$ $best [u] = min (dp[u][i]) \ I \in subtree (u) $$
$if \ Dis (u,i) > D_u \, U can't rely on I to fire $
$if \ Dis (u,i) \leq D_u, this time you can rely on I to fire, u son can also rely on I to fire, son can also rely on their own internal points to fire $
$ initialization condition \ Dp[u][i] = Dis (u,i) \leq limit[u]?cost[u]:+\infty$
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intMAXN =1010;6 Const intINF =0x3f3f3f3f;7 structarc{8 intTo,w,next;9Arcintx =0,inty =0,intz =-1){Tento =x; OneW =y; ANext =Z; - } -}e[maxn*2]; the intHead[maxn],w[maxn],d[maxn],best[maxn],dis[maxn][maxn],tot,n; - intdp[maxn][maxn],m; - voidAddintUintVintW) { -E[tot] =arc (V,w,head[u]); +Head[u] = tot++; - } + voidDfsintUintFaintDdintroot) { ADis[root][u] =DD; at for(inti = Head[u]; ~i; i =E[i].next) { - if(e[i].to = = FA)Continue; -DFS (E[I].TO,U,DD +e[i].w,root); - } - } - voidSolveintUintFA) { in for(inti =1; I <= N; ++i) - if(Dis[u][i] <= d[u]) dp[u][i] =W[i]; to for(inti = Head[u]; ~i; i =E[i].next) { + if(e[i].to = = FA)Continue; - solve (e[i].to,u); the for(intj =1; J <= N; ++j) * if(Dis[u][j] <=D[u]) $DP[U][J] + = min (dp[e[i].to][j)-w[j],best[e[i].to]);Panax Notoginseng } - for(inti =1; I <= N; ++i) Best[u] =min (best[u],dp[u][i]); the } + intMain () { A intKase,u,v,ww; thescanf"%d",&Kase); + while(kase--){ -memset (head,-1,sizeofhead); $scanf"%d",&n); $ for(inti =1; I <= N; ++i) -scanf"%d", W +i); - for(inti =1; I <= N; ++i) thescanf"%d", D +i); -tot =0;Wuyi for(inti =1; I < n; ++i) { thescanf"%d%d%d",&u,&v,&ww); - Add (U,V,WW); Wu Add (V,U,WW); - } AboutMemset (DP,0x3f,sizeofDP); $memset (Best,0x3f,sizeofBest ); - for(inti =1; I <= N; ++i) DFS (i,-1,0, i); -Solve1,-1); -printf"%d\n", best[1]); A } + return 0; the}
View Code
POJ 2152 Fire