HDU 5956 The Elder (樹上斜率DP)
阿新 • • 發佈:2017-10-15
continue 進行 clas pri 給定 題意 sig 復雜度 long
題意:給定上一棵樹,然後每條邊有一個權值,然後每個點到 1 的距離有兩種,第一種是直接回到1,花費是 dist(1, i)^2,還有另一種是先到另一個點 j,然後兩從 j 向1走,當然 j 也可以再向 k,一直到1,但經過一個點,那麽就會出多一個花費 p,問你每個點到 1 的最小距離的最大值是多少。
析:很容易想到狀態方程是 dp[i] = min{ dp[j] + (dist(1, i) - dist(1, i))^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\n", ans); } return 0; }
HDU 5956 The Elder (樹上斜率DP)