hdu5956 The Elder (树型dp, 斜率优化)

传送门:The Elder - HDU 5956 - Virtual Judge

Once upon a time, in the mystical continent, there is a frog kingdom, ruled by the oldest frog, the Elder. The kingdom consists of N cities, numbered from east to west. The 1-th city, which is located to the east of others, is the capital. Each city, except the capital, links none or several cities to the west, and exactly one city to the east.
There are some significant news happening in some cities every day. The Elder wants to know them as soon as possible. So, that is the job of journalist frogs, who run faster than any other frog. Once some tremendous news happen in a city, the journalist in that city would take the message and run to the capital. Once it reach another city, it can either continue running, or stop at that city and let another journalist to transport. The journalist frogs are too young and simple to run a long distance efficiently. As a result, it takes L^2L2 time for them to run through a path of length L. In addition, passing message requires P time for checking the message carefully, because any mistake in the message would make the Elder become extremely angry.
Now you are excited to receive the task to calculate the maximum time of sending a message from one of these cities to the capital.

Input

The first line of input contains an integer t, the number of test cases. t test cases follow. For each test case, in the first line there are two integers N (N ≤ 100000) and P (P ≤ 1000000). In the next N-1 lines, the i-th line describes the i-th road, a line with three integers u,v,w denotes an edge between the u-th city and v-th city with length w(w ≤ 100).

Output

For each case, output the maximum time.

Sample

InputcopyOutputcopy
 
3 6 10 1 2 4 2 3 5 1 4 3 4 5 3 5 6 3 6 30 1 2 4 2 3 5 1 4 3 4 5 3 5 6 3 6 50 1 2 4 2 3 5 1 4 3 4 5 3 5 6 3
 
51 75 81 

Hint

In the second case, the best transportation time is:
•  The 2-th city: 16 = 4^2
•  The 3-th city: 72 = 4^2 + 30 + 5^2
•  The 4-th city: 9 = 3^2
•   The 5-th city: 36 = (3 + 3)^2
•   The 6-th city: 75 = (3 + 3)^2 +30 + 3^2
Consequently, the news in the 6-th city requires most time to reach the capital.

AC代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;

const int N = 1e5 + 100, M = N << 1;

int h[N], ne[M], e[M], w[M], idx;
int que[N];
ll a[N], dp[N], f[N], m, ans;

ll power2(ll x) {
	return x * x;
}

void add(int a, int b, int c) {
	e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}

void dfs1(int x, int fa = -1){
	for (int i = h[x]; ~i; i = ne[i]) {
		int u = e[i];
		if (u == fa) continue;
		a[u] = a[x] + w[i];
		dfs1(u, x);
	}
}

void dfs(int x, int fa, int hh, int tt) {
	while (hh < tt && f[que[hh + 1]] - f[que[hh]] <= (a[que[hh + 1]] - a[que[hh]]) * (2 * a[x])) hh++;
	dp[x] = dp[que[hh]] + power2(a[x] - a[que[hh]]) + m;
	ans = max(ans, dp[x]);
	f[x] = dp[x] + power2(a[x]);
	while (hh < tt && (f[que[tt]] - f[que[tt - 1]]) * (a[x] - a[que[tt - 1]]) >= (f[x] - f[que[tt - 1]]) * (a[que[tt]] - a[que[tt - 1]])) tt--;
	int pre = que[++tt];  que[tt] = x;
	
	for (int i = h[x]; ~i; i = ne[i]) {
		int u = e[i];
		if (u == fa) continue;
		dfs(u, x, hh, tt);
	}
	que[tt] = pre;
}

void solve() {
	int n;
	scanf("%d%lld", &n, &m);
	memset(h, -1, sizeof(int) * (n + 10)); idx = 0;

	for (int i = 1; i < n; i++) {
		int u, v, c;
		scanf("%d%d%d", &u, &v, &c);
		add(u, v, c);
		add(v, u, c);
	}
	
	que[0] = 0;
	ans = 0;
	dfs1(1, -1);
	dfs(1, -1, 0, 0);
	
	printf("%lld\n", ans - m);
}


int main() {
	//ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int t = 1;
	scanf("%d", &t);
	while (t--) solve();
	return 0;
}

做这题的总结:

    这题求从其他点到1号点的最长距离的最小值,先推出一个dp式子,
    看到式子一般只有数据结构,单调队列,斜率优化。
    这题既有平方又有一次方,导致斜率不能直接求,可以用dp[j] < dp[k], 到i的距离写出式子
    然后就可以分离出j, i。
    由于是树型dp,可以改成从1号点到其他点的距离,由于是一颗树,导致对于x,每颗子树维护的队列都是不同的
    就会需要回溯。
    最后还有记得初始化ans,wa了好几次了呜呜

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值