【poj2152】Fire 树形DP
来源:互联网 发布:听音识曲哪个软件更好 编辑:程序博客网 时间:2024/05/20 05:56
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
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
Source
POJ Monthly,Lou Tiancheng
题意:给你一颗树,边上权值表示距离,一个点上可以建消防站,花费为
神一般的解法
设
因为
具体来说就是若v可以保护u,则
若v不可以保护u,则dp[u][v]=INF。
算完之后,
要先递归,回溯的时候计算…
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<queue>using namespace std;const int INF = 1000000010;const int SZ = 2010;int cost[SZ],d[SZ],n;int dist[SZ][SZ];int head[SZ],nxt[SZ],tot = 1;struct edge{ int t,d;}l[SZ];void build(int f,int t,int d){ l[++ tot].t = t; l[tot].d = d; nxt[tot] = head[f]; head[f] = tot;}queue<int> q;void getdist(int dist[],int s){ dist[s] = 0; q.push(s); while(q.size()) { int u = q.front(); q.pop(); for(int i = head[u];i;i = nxt[i]) { int v = l[i].t; if(!dist[v] && v != s) dist[v] = dist[u] + l[i].d,q.push(v); } }}int dp[SZ][SZ],ans[SZ];void dfs(int u,int fa){ for(int i = head[u];i;i = nxt[i]) { int v = l[i].t; if(v == fa) continue; dfs(v,u); } for(int v = 1;v <= n;v ++) { if(dist[u][v] <= d[u]) { int tmp = 0; for(int i = head[u];i;i = nxt[i]) { int k = l[i].t; if(k == fa) continue; tmp += min(dp[k][v] - cost[v],ans[k]); } dp[u][v] = cost[v] + tmp; } else dp[u][v] = INF; } for(int i = 1;i <= n;i ++) ans[u] = min(ans[u],dp[u][i]);}void init(){ tot = 1; memset(head,0,sizeof(head)); memset(dp,0,sizeof(dp)); memset(dist,0,sizeof(dist)); memset(ans,63,sizeof(ans));}int main(){ int T; scanf("%d",&T); while(T --) { init(); scanf("%d",&n); 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 - 1;i ++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); build(a,b,c); build(b,a,c); } for(int i = 1;i <= n;i ++) getdist(dist[i],i); dfs(1,0); printf("%d\n",ans[1]); } return 0;}
- POJ2152 Fire 树形DP
- poj2152(Fire) 树形DP
- 【poj2152】【Fire】【树形dp】
- 【poj2152】Fire 树形DP
- 【POJ2152】Fire——树形DP
- POJ2152 树形DP
- POJ2152 Fire
- 树形dp(Fire)
- 【POJ 2152】Fire【树形DP】
- Fire - POJ 2152 树形dp
- poj 2152 Fire 树形dp
- poj 2152 Fire 树形DP
- 【树形DP】 POJ 2152 Fire
- [树形DP] POJ 2152 Fire
- Poj 2152 Fire (DP_树形DP)
- ***POJ 2152 - Fire(树形DP)
- poj 2152 Fire - 经典树形dp
- POJ 2152 Fire (树形DP,有趣)
- HDOJ 2073-无限的路
- 基于SAE和lanewechat开源PHP微信框架开发微信公众平台(一)成功启用服务器配置
- c++实现线程池
- 判断是否是平衡二叉树
- 51nod1083 矩阵取数问题
- 【poj2152】Fire 树形DP
- 经典题:不断求第k大数(巧用优先队列)(网赛)(4006)
- HDU2563——统计问题
- iOS中的单例模式
- linux c 实现线程池
- Markdown的最基本最常用的语法
- java日志组件介绍(common-logging,log4j,slf4j,logback )
- HDOJ 2186-悼念512汶川大地震遇难同胞——一定要记住我爱你
- linux 安装 nginx