zoukankan      html  css  js  c++  java
  • POJ 2152 Fire (树形DP)

    题意:给定一棵树,要建立一些消防站,并且每个结点到最近一个的消防站的距离不能超过limit i,在每个结点建立消防站要花一定的费用cost i,求最少的花费是多少。

    析:想了很久,确实是没想出来怎么做,dp[i][j] 表示 i 结点依赖 j 结点的最小花费,然后ans[i] 表示 以 i 为根结点的树,最少花费。在转移时,就是直接枚举 i 结点,所以依赖的 j 结点,然后ans 是取最小值,dp[i][j] += min{ dp[k][j] - cost[j], ans[k] }。其中 k 是 i 的子结点,因为 i 结点依赖 j ,其子树也是可以 j 结点的。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1000 + 50;
    const int mod = 1000;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r > 0 && r <= n && c > 0 && c <= m;
    }
    
    struct Edge{
      int to, next, val;
    };
    Edge edge[maxn<<1];
    int head[maxn], cnt;
    int cost[maxn], limit[maxn];
    
    void addEdge(int u, int v, int c){
      edge[cnt].to = v;
      edge[cnt].val = c;
      edge[cnt].next = head[u];
      head[u] = cnt++;
    }
    
    int dist[maxn][maxn];
    int ans[maxn], dp[maxn][maxn];
    
    void dfs_for_dist(int u, int fa, int sp){
      for(int i = head[u]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(v == fa)  continue;
        dist[sp][v] = dist[v][sp] = dist[sp][u] + edge[i].val;
        dfs_for_dist(v, u, sp);
      }
    }
    
    void dfs(int u, int fa){
      for(int i = head[u]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(v == fa)  continue;
        dfs(v, u);
      }
      for(int j = 1; j <= n; ++j){
        if(dist[u][j] > limit[u])  continue;
        dp[u][j] = cost[j];
        for(int i = head[u]; ~i; i = edge[i].next){
          int v = edge[i].to;
          if(v == fa)  continue;
          dp[u][j] += min(ans[v], dp[v][j] - cost[j]);
        }
        ans[u] = min(ans[u], dp[u][j]);
      }
    }
    
    int main(){
      int T;  cin >> 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);
        ms(head, -1);  cnt = 0;
        for(int i = 1; i < n; ++i){
          int u, v, c;
          scanf("%d %d %d", &u, &v, &c);
          addEdge(u, v, c);
          addEdge(v, u, c);
        }
        for(int i = 1; i <= n; ++i)  dfs_for_dist(i, -1, i);
        ms(ans, INF);  ms(dp, INF);
        dfs(1, -1);
        printf("%d
    ", ans[1]);
      }
      return 0;
    }
    

      

  • 相关阅读:
    一些你不知道的js特性【一】
    webpack打包优化实践
    prerender-spa-plugin预渲染踩坑
    前端学习中你必须了解的几张图
    fis3打包中的一些注意事项
    js中的运算符优先级
    Vivado安装教程
    ISE14.7使用教程(一个完整工程的建立)
    如何高效的编写Verilog HDL——进阶版
    Vivado 2017封装自定义IP Core
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7486943.html
Copyright © 2011-2022 走看看