poj 2152 Fire

本文介绍了一种解决城市消防站最优布局问题的算法。针对N个城市节点,通过计算每个节点建立消防站的成本及到最近消防站的距离限制,确定使得总成本最低的消防站建设方案。算法使用深度优先搜索与动态规划技术实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fire
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

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

见—灰色果实

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int vet[4010],val[4010],Next[4010];
int hed[4010],vis[4010];
int dis[4010][4010],dp[4010][4010];
int tim[4010],lim[4010],bst[4010];
int q[8020];
int num;
int n;
void add(int u,int v,int z){
  ++num;
  vet[num]=v;
  val[num]=z;
  Next[num]=hed[u];
  hed[u]=num;
}

void dfs(int x,int dis[]){
  int head=0,tail=0;
  q[0]=x;
  vis[x]=x;
  dis[x]=0;
  while (head<=tail){
    int u=q[head];
    ++head;
    for (int i=hed[u];i!=-1;i=Next[i]){
      int v=vet[i],z=val[i];
      if (vis[v]!=x){
        vis[v]=x;
        dis[v]=dis[u]+z;
        ++tail;
        q[tail]=v;
      }
    }
  }
}
void check(int x,int fa){
  for (int i=hed[x];i!=-1;i=Next[i]){
    int v=vet[i];
    if (v==fa) continue;
    check(v,x);
  }
  for (int i=1;i<=n;++i)
  if (dis[x][i]<=lim[x]){
    dp[x][i]=0;
    for (int j=hed[x];j!=-1;j=Next[j]){
      int v=vet[j];
      if (v==fa) continue;
      dp[x][i]+=min(dp[v][i]-tim[i],bst[v]);
    }
    dp[x][i]+=tim[i];
    bst[x]=min(bst[x],dp[x][i]);
  }
}
int main(){
 int t;
 scanf("%d",&t);
 while (t--){
  num=0;
  scanf("%d",&n);
  for (int i=1;i<=n;++i) scanf("%d",&tim[i]);
  for (int i=1;i<=n;++i) scanf("%d",&lim[i]);
  for (int i=1;i<=n;++i){
    hed[i]=-1;
    vis[i]=0;
    bst[i]=1<<30;
    for (int j=1;j<=n;++j) {dp[i][j]=1<<30;dis[i][j]=0;}
  }
  for (int i=1;i<n;++i){
    int u,v,z;
    scanf("%d%d%d",&u,&v,&z);
    add(u,v,z);
    add(v,u,z);
  }
  for (int i=1;i<=n;++i) dfs(i,dis[i]);
  check(1,0);
  printf("%d\n",bst[1]);
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值