• HDU 5956 The Elder (树上斜率DP)


    题意:给定上一棵树,然后每条边有一个权值,然后每个点到 1 的距离有两种,第一种是直接回到1,花费是 dist(1, i)^2,还有另一种是先到另一个点 j,然后两从 j 向1走,当然 j 也可以再向 k,一直到1,但经过一个点,那么就会出多一个花费 p,问你每个点到 1 的最小距离的最大值是多少。

    析:很容易想到状态方程是 dp[i] = min{ dp[j] + (dist(1, i) - dist(1, j))^2 + P } dist(1, i) 表示 1 到 i 的距离。但可以是斜率进行优化, 这样的话,时间复杂度就小了,只不是树上的而已。

    代码如下:

    #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(i,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<LL, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 50;
    const int maxm = 1e6 + 5;
    const int mod = 50007;
    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, dist, next;
    };
    Edge edge[maxn<<1];
    int head[maxn], cnt;
    
    void addEdge(int u, int v, int dist){
      edge[cnt].to = v;
      edge[cnt].dist = dist;
      edge[cnt].next = head[u];
      head[u] = cnt++;
    }
    
    LL dp[maxn];
    
    struct Node{
      int num, pos, time;
    };
    stack<Node> st;
    int q[maxn], dfs_cnt, fro, rear;
    LL sum[maxn];
    
    LL DP(int i, int j){
      return dp[j] + sqr(sum[i]-sum[j]) + m;
    }
    
    LL UP(int j, int k){
      return dp[j] + sqr(sum[j]) - dp[k] - sqr(sum[k]);
    }
    
    LL DOWN(int j, int k){
      return 2LL * (sum[j] - sum[k]);
    }
    
    void dfs(int u, int fa){
      int ti = ++dfs_cnt;
      while(fro + 1 < rear && UP(q[fro+2], q[fro+1]) <= sum[u] * DOWN(q[fro+2], q[fro+1]))  ++fro;
      dp[u] = DP(u, q[fro+1]);
      while(fro + 1 < rear && UP(u, q[rear]) * DOWN(u, q[rear-1]) <= UP(u, q[rear-1]) * DOWN(u, q[rear])){
        Node tmp;
        tmp.pos = rear;
        tmp.num = q[rear];
        tmp.time = dfs_cnt;
        st.push(tmp);
        --rear;
      }
      q[++rear] = u;
      int nowfro = fro, nowrear = rear;
      for(int i = head[u]; ~i; i = edge[i].next){
        int v = edge[i].to;
        if(v == fa)  continue;
        sum[v] = sum[u] + edge[i].dist;
        fro = nowfro;  rear = nowrear;
        while(!st.empty()){
          Node tmp = st.top();
          if(tmp.time <= ti)  break;
          q[tmp.pos] = tmp.num;
          st.pop();
        }
        dfs(v, u);
      }
    }
    
    int main(){
      int T;  cin >> T;
      while(T--){
        scanf("%d %d", &n, &m);
        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);
        }
        dp[1] = -m;
        q[++rear] = 1;
        dfs_cnt = 0;
        for(int i = head[1]; ~i; i = edge[i].next){
          int v = edge[i].to;
          fro = 0, rear = 1;
          while(!st.empty())  st.pop();
          sum[v] = sum[1] + edge[i].dist;
          dfs(v, 1);
        }
        LL ans = 0;
        for(int i = 1; i <= n; ++i)  ans = max(ans, dp[i]);
        printf("%I64d
    ", ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    flex
    IOCP三:多个接收
    IOCP三:多个接收
    IOCP二:同时发送和接收
    IOCP二:同时发送和接收
    IOCP一:AcceptEx
    IOCP一:AcceptEx
    字符串和数字相互转换
    字符串和数字相互转换
    QThread应用详解
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7672742.html
Copyright © 2020-2023  润新知