Question link: http://poj.org/problem? Id = 2152
A tree with Edge Weight indicates the distance between nodes. The price for building a fire station on I is W [I]. If a fire station is not built on I, the distance between it and the fire station closest to this point cannot be greater than D [I]. To meet the minimum cost of website construction;
Solution; it was difficult to see Chen Qifeng's paper. However, I have discussed the situation in the paper and I should not need it; DP [I] [J] indicates that J is selected as the supply station at I (but it is not necessarily the most recent, so even if it is closer, it doesn't matter, and in the paper .), Best [I] indicates the minimum cost for I AND ITS subtree to meet the requirements. The transfer equation is as follows:
Best [I] = min (DP [I] [J]), 1 <= j <= N
DP [I] [J] = W [J] + min (DP [k] [J]-W [J], best [k]), k is the son of I
Code:
/******************************************************* @author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>//freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8#define zero(_) (abs(_)<=eps)const double pi=acos(-1.0);typedef long long LL;const int Max=100010;const int INF=1e9+7;int n;int cost[1010];int d[1010];int dist[1010];int dp[1010][1010];int best[1010];vector<pair<int,int> > vec[1010];void add(int a,int b,int c){ vec[a].push_back(pair<int,int>(b,c)); vec[b].push_back(pair<int,int>(a,c));}void getdist(int u,int before,int add){ dist[u]=dist[before]+add; for(int i=0; i<vec[u].size(); i++) if(vec[u][i].first!=before) getdist(vec[u][i].first,u,vec[u][i].second);}void dfs(int u,int before){ //cout<<u<<" "<<before<<endl; for(int i=0; i<vec[u].size(); i++) { if(vec[u][i].first==before) continue; dfs(vec[u][i].first,u); } dist[u]=0; getdist(u,0,0); for(int i=1; i<=n; i++) if(dist[i]<=d[u]) { dp[u][i]=cost[i]; for(int k=0; k<vec[u].size(); k++) if(vec[u][k].first!=before) dp[u][i]+=min(dp[vec[u][k].first][i]-cost[i],best[vec[u][k].first]); best[u]=min(best[u],dp[u][i]); } else dp[u][i]=INF;}int main(){ int t; cin>>t; while(t--) { scanf("%d",&n); for(int i=0; i<=n; i++) vec[i].clear(); 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=0; i<n-1; i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add(a,b,c); } for(int i=1; i<=n; i++) best[i]=INF; dfs(1,0); cout<<best[1]<<endl; } return 0;}