https://cn.vjudge.net/problem/POJ-2152
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
//题意: 现在有些城市着火了, 需要建立一定数量的消防站, 每一个点建站有一个不同的花费, 有边权, 如果一个城市没有建立消防站 , 那么至少离他一个limit的距离比较有个消防站, 不同的点limit也不同, 问满足条件的最小花费是多少?
//思路: 我们并不能通过一次遍历而得到我们需要的答案. 我们可以考虑暴力判断每一个点是否可以作为当前点的依附点(即在这个点建立消防站), 那么我们就有一个n^2的算法, 综合数据发现, 这个n最大也只是1000, 所以n^2是完全够的, 为了实现这个求解我们需要一些辅助数组.
best[x] 代表的是x的子树下满足题意的最小花费是多少. 显然最后答案就是best[1].
dp[u]][j] 代表的是u依附于j时u的子树内满足条件的最小花费是多少. (j就是我们暴力判断的每一个点)
转移方程就是:
best[x] = min(dp[x][j],best[x]);
dp[u][j] = sum( min(dp[v][j] - cost[j], best[v]) );
那么就可以写啦
---------------------
作者:Anxdada
来源:优快云
原文:https://blog.youkuaiyun.com/Anxdada/article/details/78273756
版权声明:本文为博主原创文章,转载请附上博文链接!
//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e3+5;
const int INF=0x3f3f3f3f;
int head[N],cnt=0;
int dis[N][N];
int cost[N],limit[N];
int root,n;
int dp[N][N];//dp[u]][j] 代表的是u依附于j时u的子树内满足条件的最小花费是多少.
int best[N];//best[x] 代表的是x的子树下满足题意的最小花费是多少. 显然最后答案就是best[1].
struct node{
int to,w,next;
}e[N<<1];
void init(){
cnt=0;
for(int i=1;i<=n;i++){
head[i]=-1;
best[i]=INF;
for(int j=1;j<=n;j++) dp[i][j]=INF;
}
}
void add(int u,int v,int w){
e[cnt].to=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt++;
}
void dfs_dis(int u,int fa,int len){
dis[root][u]=len;
for(int i=head[u];~i;i=e[i].next){
int &v=e[i].to;
if(v==fa) continue;
dfs_dis(v,u,len+e[i].w);
}
}
void dfs(int u,int fa){
for(int i=head[u];~i;i=e[i].next){
int &v=e[i].to;
if(v==fa) continue;
dfs(v,u);
}
for(int i=1;i<=n;i++){
if(dis[u][i]>limit[u]) continue;//距离不符合
dp[u][i]=cost[i];
for(int j=head[u];~j;j=e[j].next){
int &v=e[j].to;
if(v==fa) continue;
dp[u][i]+=min(dp[v][i]-cost[i],best[v]);
}
best[u]=min(dp[u][i],best[u]);
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&cost[i]);
for(int i=1;i<=n;i++) scanf("%d",&limit[i]);
init();
for(int i=1;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
for(int i=1;i<=n;i++){//O(n^2)处理任意两点的距离
root=i;
dfs_dis(i,-1,0);
}
dfs(1,-1);
printf("%d\n",best[1]);
}
return 0;
}
本文探讨了一种解决消防站布局问题的算法,目标是在满足每个城市与最近消防站距离限制的前提下,找到建立消防站的最小总成本。通过深度优先搜索和动态规划策略,文章详细解释了如何计算最优解。
7341

被折叠的 条评论
为什么被折叠?



