POJ 2152.Fire 樹形dp
阿新 • • 發佈:2017-10-06
front single define imu pat cheng font void 樹形 Fire
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.
For each test case output the minimum cost on a single line.
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 1616 | Accepted: 878 |
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 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
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 題目鏈接:http://poj.org/problem?id=2152 題意一個國家有n個城市,城市之間的道路是樹狀結構。現在要修一些消防站,在城市建設消防站花費為w[i],每個城市必須離消防站的距離小於d[i]。求建立完善的消防站需要至少花費多少。 思路:樹形dp。dp[u][v]表示u城市依賴v城市建設的消防站。dp[u][v]+=dp[son][v]-w[v],其中son表示u的兒子。ans[u]表示u為根時,最優答案。 代碼:#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<map> #include<stack> #include<set> #include<bitset> using namespace std; #define PI acos(-1.0) #define eps 1e-8 typedef long long ll; typedef pair<int,int > P; const int N=1e3+100,M=2e6+100; const int inf=0x3f3f3f3f; const ll INF=1e13+7,mod=1e9+7; struct edge { int from,to; int w; int next; }; edge es[M]; int cnt,head[N]; int n; int w[N],d[N]; int dist[N][N]; int dp[N][N],ans[N]; void init() { cnt=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int w) { cnt++; es[cnt].from=u,es[cnt].to=v; es[cnt].w=w; es[cnt].next=head[u]; head[u]=cnt; } void bfs(int s) { queue<int>q; dist[s][s]=0; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u]; i!=-1; i=es[i].next) { edge e=es[i]; if(dist[s][e.to]>dist[s][u]+e.w) { dist[s][e.to]=dist[s][u]+e.w; q.push(e.to); } } } } void dfs(int u,int fa) { for(int i=head[u]; i!=-1; i=es[i].next) { int v=es[i].to; if(v==fa) continue; dfs(v,u); } for(int v=1; v<=n; v++) { if(dist[u][v]>d[u]) continue; dp[u][v]=w[v]; for(int i=head[u]; i!=-1; i=es[i].next) { edge e=es[i]; if(e.to==fa) continue; dp[u][v]+=min(dp[e.to][v]-w[v],ans[e.to]); } ans[u]=min(ans[u],dp[u][v]); } } int main() { int T; scanf("%d",&T); while(T--) { init(); scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&w[i]); for(int i=1; i<=n; i++) scanf("%d",&d[i]); for(int i=1; i<n; i++) { int u,v,l; scanf("%d%d%d",&u,&v,&l); addedge(u,v,l),addedge(v,u,l); } memset(dist,inf,sizeof(dist)); memset(dp,inf,sizeof(dp)); memset(ans,inf,sizeof(ans)); for(int i=1; i<=n; i++) bfs(i); dfs(1,0); printf("%d\n",ans[1]); } return 0; }樹形dp
POJ 2152.Fire 樹形dp