Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 972 | Accepted: 463 |
Description
Input
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 题意:有n个地区,构成一棵树,由于容易发生火灾,因此需要修消防站来防止火灾发生,在每一个地区修建消防站需要的花费,以及假如这个地区没有消防站,最近的消防站 与该地区的最大距离,求修建消防站的最小花费。 好难的树形dp。看了别人的题解一下午,终于看懂了,dp[i][j]表示以在j点有消防站时,i为根的所有子树收到消防站的保护所需的最小花费,best[i]表示 以i为根的子树收到消防站保护所需的最小花费,树形dp的同时处理当前节点到所有点的距离,然后就可以进行状态转移了。 代码:/* *********************************************** Author :xianxingwuguan Created Time :2014-2-4 19:07:09 File Name :1.cpp ************************************************ */ #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <limits.h> #include <string> #include <time.h> #include <math.h> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define INF 1000000000; #define eps 1e-8 #define pi acos(-1.0) typedef long long ll; const int maxn=1010; int dist[maxn],head[maxn],tol,dis[maxn],weight[maxn],n,best[maxn],dp[maxn][maxn]; struct node{ int next,to,val; }edge[3*maxn]; void add(int u,int v,int val){ edge[tol].to=v; edge[tol].next=head[u]; edge[tol].val=val; head[u]=tol++; } void bfs(int u) { for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(dist[v]!=-1)continue; dist[v]=dist[u]+edge[i].val; bfs(v); } } void dfs(int u,int fa){ for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(v==fa)continue; dfs(v,u); } for(int i=1;i<=n;i++)dist[i]=-1; dist[u]=0;bfs(u); best[u]=INF; for(int i=1;i<=n;i++) { if(dist[i]>dis[u]){ dp[u][i]=INF; } else { dp[u][i]=weight[i]; for(int j=head[u];j!=-1;j=edge[j].next){ int v=edge[j].to; if(v==fa)continue; dp[u][i]+=min(best[v],dp[v][i]-weight[i]); } best[u]=min(best[u],dp[u][i]); } } } int main() { // freopen("data.txt","r",stdin); //freopen("data.out","w",stdout); int i,j,k,m,T; scanf("%d",&T); while(T--){ scanf("%d",&n); memset(head,-1,sizeof(head));tol=0; for(i=1;i<=n;i++) scanf("%d",&weight[i]); for(i=1;i<=n;i++) scanf("%d",&dis[i]); for(i=1;i<n;i++){ scanf("%d%d%d",&j,&k,&m); add(j,k,m); add(k,j,m); } dfs(1,-1); cout<<best[1]<<endl; } return 0; }
原文:http://blog.csdn.net/xianxingwuguan1/article/details/18924837