"poj2152" "Fire" "Tree DP"

Source: Internet
Author: User

Kindle
Time limit:2000ms Memory limit:65536k
Total submissions:1161 accepted:595
Description

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.
Input

The 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.
Output

For each test case output, the minimum cost of a single line.
Sample Input

5
5
1 1 1) 1 1
1 1 1) 1 1
1 2 1
2 3 1
3 4 1
4 5 1
5
1 1 1) 1 1
2 1 1) 1 2
1 2 1
2 3 1
3 4 1
4 5 1
5
1 1 3) 1 1
2 1 1) 1 2
1 2 1
2 3 1
3 4 1
4 5 1
4
2 1 1 1
3 4 3 2
1 2 3
1 3 3
1 4 2
4
4 1 1 1
3 4 3 2
1 2 3
1 3 3
1 4 2
Sample Output

2
1
2
2
3

Test instructions: Given a tree of n nodes, the tree has edge rights. Now to build a fire station at some point, each point to build a station has a cost[i], if not at the current point of construction station, but also rely on other fire stations, and distance not more than limit[i]. The minimum cost to build a station plan that meets the above conditions. n <= 1000.

This problem is a comparison of God's tree-shaped DP, I was watching the Chen Qi 2006 National Training Team paper will be: "Yichi, the way to solve the problem-" about the system, relaxation "method in the application of the problem solving"
since there are already papers, I would simply say:
as a normal way of thinking, We maintain best[i] represents the minimum cost of nodes at node I.
But we find that there is a comparison of this problem (Dan) Teng that he has a limit. So the rules we've come up with are more difficult to solve.
Then we can think of this restriction is actually to find the closest to the node I fire station on it.
But that's it, we're still more difficult to write this transfer equation ...
We think further, we don't really need to find the nearest node, just need to know the fire station within the limits of I. --This step is what the paper says about the relaxation requirements.
First we need to know a property: Assuming that all the cities in the p1->pm of the station are PI nodes, then there is always an optimal solution to meet the above-mentioned properties.
As for the proof I am not here to repeat, everyone to read the paper. The
assumes that we have got this property, wondering if we can save such an array
F[i][j] to indicate that at node I, I select J Point as the optimal value for the station of node I.
Then we can divide the transfer equation into a few stages:
①: When Dis[i][j]>limit[i] we don't need to take care of him.
②: When Dis[i][j]<=limit[i] can also be divided into a few stages:
(1): When J is not a subtree of I, then I have a child node x there are two choices, one is to select the X sub-tree fire station, then f[i][j]=best[x]. The other is to choose nodes outside of X as the responsible station, according to the nature of the above we can know that I is responsible for the same station as X, then F[i][j]=f[x][j].
F[i][j]+=min (f[x][j],best[x]);
(2): Select the I node to build the fire station.
F[i][i]+=cost[i]+min (Best[x],f[x][i]);
(3): Select the sub-tree of the I node as the responsible station, in fact, this is similar to the first kind of
F[i][j]+=min (F[k][i]-cost[j],best[k]) +cost[j];
So we can solve it later.

#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace STD;Const intn=1010;Const intinf=210000000;structs{intV,len;} Point vector <S>Tr[n];intT,n,best[n],dis[n][n],f[n][n],cost[n],limit[n],now;/ *------to preprocess the distance between two points--------* / voidDfsintXintLastintLen) {intI,j,v,len; Dis[now][x]=len; for(i=0; I<tr[x].size (); ++i) {v=tr[x][i].v; Len=tr[x][i].len;if(v!=last) DFS (V,x,len+len); }}voiddpintXintLast) {intI,j,v,len; for(i=0; I<tr[x].size (); ++i)if(Tr[x][i].v!=last) DP (TR[X][I].V,X); for(i=1; i<=n;++i) {if(Dis[x][i]<=limit[x]) {F[x][i]=cost[i]; for(j=0; J<tr[x].size (); ++j) {v=tr[x][j].v; Len=tr[x][j].len;if(v!=last) F[x][i]+=min (F[v][i]-cost[i],best[v]);//(2), (3)} best[x]=min (Best[x],f[x][i]);//(1) Cases}    }}intMain () {scanf("%d", &t); while(t--) {intI,j,x,y,z;scanf("%d", &n); for(i=1; i<=n;++i)scanf("%d", &cost[i]); for(i=1; i<=n;++i)scanf("%d", &limit[i]); for(i=1; i<=n;++i) {tr[i].clear ();        Best[i]=inf; } for(i=1; i<=n;++i) for(j=1; j<=n;++j) F[i][j]=inf; for(i=1; i<n;++i) {scanf("%d%d%d", &x,&y,&z);            Point.v=y;point.len=z;            Tr[x].push_back (point);            Point.v=x;point.len=z;        Tr[y].push_back (point); } for(i=1; i<=n;++i) {now=i; DFS (I,0,0); } DP (1,0);printf("%d\n", best[1]); }}

"poj2152" "Fire" "Tree DP"

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.