SPOJ FTOUR2 Free tour II

Description

After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, Travel Agent SPOJ goes on with another discount tour.

The tour will be held on ICPC island, a miraculous one on the Pacific Ocean. We list N places (indexed from 1 to N) where the visitors can have a trip. Each road connecting them has an interest value, and this value can be negative (if there is nothing interesting to view there). Simply, these N places along with the roads connecting them form a tree structure. We will choose two places as the departure and destination of the tour.

Since September is the festival season of local inhabitants, some places are extremely crowded (we call them crowded places). Therefore, the organizer of the excursion hopes the tour will visit at most K crowded places (too tiring to visit many of them) and of course, the total number of interesting value should be maximum.

Briefly, you are given a map of N places, an integer K, and M id numbers of crowded place. Please help us to find the optimal tour. Note that we can visit each place only once (or our customers easily feel bored), also the departure and destination places don't need to be different.

Input

There is exactly one case. First one line, containing 3 integers N K M, with 1 <= N <= 200000, 0 <= K <= M, 0 <= M <= N.

Next M lines, each line includes an id number of a crowded place.

The last (N - 1) lines describe (N - 1) two-way roads connected N places, form a b i, with a, b is the id of 2 places, and i is its interest value (-10000 <= i <= 10000).

Output

Only one number, the maximum total interest value we can obtain.

Example

Input:
8 2 3
3
5
7
1 3 1
2 3 10
3 4 -2
4 5 -1
5 7 6
5 6 5
4 8 3


Output:
12

Explanation

We choose 2 and 6 as the departure and destination place, so the tour will be 2 -> 3 -> 4 -> 5 -> 6, total interest value = 10 + (-2) + (-1) + 5 = 12

* Added some unofficial cases

hide comments 
 
 
w703710691d2015-09-04 15:55:24 

why I always get a re?

lu2015-06-01 17:08:24 

the result turned to TLE QAQ

lu2015-06-01 16:03:01 

my program passed the following tests but it got WA.I don't know what's wrong

kid2015-01-07 15:17:10 

I chanllenged many codes using the following input 
2 0 0 
1 2 1 
pay attention to the initialization

yzx2012-09-23 07:05:00 

answer:6

Exia_cai2012-09-14 08:07:12 

here is a test: 
5 3 4 




1 2 1 
2 3 3 
1 4 1 
1 5 2 

谢良2011-06-09 12:31:58 

300

Zhao Tao2010-04-27 03:13:28 

The test data of this problem is somewhat weak. I successfully chanllenged a code which was accepted using the following input: 
7 5 6 






1 7 100 
1 5 100 
5 6 100 
1 2 1 
2 3 1 
3 4 1


Hint

Added by:Thanh-Vy Hua
Date:2007-09-28
Time limit:0.100s-0.443s
Source limit:50000B
Memory limit:1536MB
Cluster:Cube (Intel G860)
Languages:All except: ERL JS NODEJS PERL 6 VB.net
Resource:Adapted from Preslav Le's problem, first used in Bulgarian OI 07
树分治,统计一下每个点出发的经过的点数与距离,我这里选择用树状数组统计前缀的最大值。
#include<queue>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int maxn = 4e5 + 10;
int n, w, m, x, y, z, f[maxn];

struct Tree
{
	int ft[maxn], nt[maxn], u[maxn], v[maxn], sz;
	int mx[maxn], ct[maxn], vis[maxn];
	int d[maxn], D[maxn], t;

	void clear(int n)
	{
		mx[sz = 0] = INF;
		for (int i = 1; i <= n; i++) ft[i] = -1, f[i] = vis[i] = 0;
	}
	void AddEdge(int x, int y, int z)
	{
		u[sz] = y;	v[sz] = z;	nt[sz] = ft[x];	ft[x] = sz++;
		u[sz] = x;	v[sz] = z;	nt[sz] = ft[y]; ft[y] = sz++;
	}
	int dfs(int x, int fa, int sum)
	{
		int y = mx[x] = (ct[x] = 1) - 1;
		for (int i = ft[x]; i != -1; i = nt[i])
		{
			if (vis[u[i]] || u[i] == fa) continue;
			int z = dfs(u[i], x, sum);
			ct[x] += ct[u[i]];
			mx[x] = max(mx[x], ct[u[i]]);
			y = mx[y] < mx[z] ? y : z;
		}
		mx[x] = max(mx[x], sum - ct[x]);
		return mx[x] < mx[y] ? x : y;
	}
	int get(int x, int fa, int k, int dep)
	{
		int y = k;
		d[k] = max(d[k], dep);
		for (int i = ft[x]; i != -1; i = nt[i])
		{
			if (vis[u[i]] || u[i] == fa) continue;
			y = max(get(u[i], x, k + f[u[i]], dep + v[i]), y);
		}
		return y;
	}
	int find(int x)
	{
		t = get(x, -1, f[x], 0) + 1;
		int ans = 0;
		for (int i = 0; i <= t; i++) D[i] = 0;
		for (int i = ft[x]; i != -1; i = nt[i])
		{
			if (vis[u[i]]) continue;
			int len = get(u[i], x, f[x] + f[u[i]], v[i]);
			for (int j = f[x] + f[u[i]]; j <= len; j++) d[j] = -INF;
			get(u[i], x, f[x] + f[u[i]], v[i]);
			for (int j = f[x] + f[u[i]]; j <= len; j++)
			{
				int Max = -INF;
				for (int k = min(w + f[x] - j + 1, t); k > 0; k -= low(k)) Max = max(Max, D[k]);
				if (Max != -INF) ans = max(ans, Max + d[j]);
			}
			for (int j = f[x] + f[u[i]]; j <= len; j++)
			{
				for (int k = j + 1; k <= t; k += low(k)) D[k] = max(D[k], d[j]);
			}
		}
		for (int k = min(w + 1, t); k > 0; k -= low(k)) ans = max(ans, D[k]);
		return ans;
	}
	int work(int x, int sum)
	{
		int y = dfs(x, -1, sum);
		int ans = find(y);	vis[y] = 1;
		for (int i = ft[y]; i != -1; i = nt[i])
		{
			if (vis[u[i]]) continue;
			ans = max(ans, work(u[i], ct[u[i]] > ct[y] ? sum - ct[y] : ct[u[i]]));
		}
		return ans;
	}
}solve;

int main()
{
	while (scanf("%d%d%d", &n, &w, &m) != EOF)
	{
		solve.clear(n);
		while (m--) scanf("%d", &x), f[x] = 1;
		for (int i = 1; i < n; i++)
		{
			scanf("%d%d%d", &x, &y, &z);
			solve.AddEdge(x, y, z);
		}
		printf("%d\n", solve.work(1, n));
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值