Description
Country Z has N cities, which are numbered from 1 to N. Cities are connected by highways, and there is exact one path between two 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 may be different. If there is not firehouse in city K, the distance between it and the nearest city which has a firehouse, can’t be more than D(K). D for different cities also may be different. To save money, the government wants you to calculate the minimum cost to build firehouses.
Input
The first line of input contains a single integer T representing the number of 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 by one or more blanks. The I-th number means W(I) (0 < W(I) <= 10000). The third line contains N numbers separated by 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 <= 1000), which means there is a highway between city u and v of length L.
Output
For each test case output the minimum cost on 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
Source
POJ Monthly,Lou Tiancheng
这道题好难啊QAQ……不看题解完全想不到啊,最后看了网上大佬的题解总算是艰难地攻克下来啦……
从开的数组可以看出这题分为几个部分:首先要读入(废话),然后预处理出两两之间的距离(用邻接矩阵存,2000×2000随便存)。这个就直接对每一点bfs一下暴力求距离就好。
之后就是最难的——树形dp!
其中用到一个二维数组dp,dp(u,v)表示节点u由节点v控制时以u为根的子树全部满足条件所需的最小花费,还有一个一维数组ans,ans(u)表示以u为根的子树全部满足条件所需的最小花费。
对于每一个节点u,遍历它的儿子节点,回溯时处理:
①对于其他每一个节点v(1~n中的任意节点),如果这个节点能够覆盖到u,就更新dp(u,v),否则将dp(u,v)设为一个极大值。更新的方法是:首先加上在v建站的花费。由于dp(u,v)表示的是这个子树的总花费,所以还需要把它的儿子的所有花费加起来。但加起来的时候要注意,由于算这个子树的时候还加上了v的花费,所以实际上要从dp(son,v)-cost(v)和ans(son)之间取个min值进行统计(不难想到如果ans(son)使用了v,那么ans的值与dp(son,v)相同,那么更新值时仍会选择dp(son,v)-cost(v))。
②对于ans数组是很好更新的,直接对所有的dp(u,v)取个min就好了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int t, n, d[2010], cost[2010];
struct N {
int v, d, next;
} e[4010];
int num = 0, head[4010];
void add(int u, int v, int d) {
num ++; e[num].v = v; e[num].d = d;
e[num].next = head[u]; head[u] = num;
}
int dis[2010][2010];
void getdis(int dis[], int s) {
int h = 0, t = 1, queue[2010];
queue[1] = s; dis[s] = 0;
while(h < t) {
int u = queue[++ h];
for(int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if(! dis[v] && v != s)
dis[v] = dis[u] + e[i].d, queue[++ t] = v;
}
}
}
int ans[2010], dp[2010][2010];
void dfs(int u, int fa) {
for(int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if(v == fa) continue ;
dfs(v, u);
}
for(int v = 1; v <= n; v ++)
if(dis[u][v] <= d[u]) {
dp[u][v] = cost[v];
for(int i = head[u]; i; i = e[i].next) {
int k = e[i].v;
if(k == fa) continue ;
dp[u][v] += min(dp[k][v] - cost[v], ans[k]);
}
}
else dp[u][v] = 0x3fffffff;
for(int v = 1; v <= n; v ++)
ans[u] = min(ans[u], dp[u][v]);
}
void init() {
num = 0;
memset(dp, 0, sizeof(dp));
memset(dis, 0, sizeof(dis));
memset(ans, 63, sizeof(ans));
memset(head, 0, sizeof(head));
}
int main() {
scanf("%d", &t);
while(t --) {
scanf("%d", &n); init();
for(int i = 1; i <= n; i ++)
scanf("%d", &cost[i]);
for(int i = 1; i <= n; i ++)
scanf("%d", &d[i]);
for(int i = 1; i < n; i ++) {
int u, v, d;
scanf("%d %d %d", &u, &v, &d);
add(u, v, d); add(v, u, d);
}
for(int i = 1; i <= n; i ++)
getdis(dis[i], i);
dfs(1, 0);
printf("%d\n", ans[1]);
}
return 0;
}