QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 128 MB Total points: 100 Hackable ✓

#17192. 疫情控制

統計

H 国有 $n$ 个城市,这 $n$ 个城市用 $n-1$ 条双向道路相互连通构成一棵树,$1$ 号城市是首都,也是树中的根节点。

H 国的首都爆发了一种危害性极高的传染病。当局为了控制疫情,不让疫情扩散到边境城市(叶子节点所表示的城市),决定动用军队在一些城市建立检查点,使得从首都到边境城市的每一条路径上都至少有一个检查点,边境城市也可以建立检查点。但特别要注意的是,首都是不能建立检查点的。

现在,在 H 国的一些城市中已经驻扎有军队,且一个城市可以驻扎多个军队。一支军队可以在有道路连接的城市间移动,并在除首都以外的任意一个城市建立检查点,且只能在一个城市建立检查点。一支军队经过一条道路从一个城市移动到另一个城市所需要的时间等于道路的长度(单位:小时)。

请问最少需要多少个小时才能控制疫情。注意:不同的军队可以同时移动。

输入格式

第一行一个整数 $n$,表示城市个数。

接下来的 $n-1$ 行,每行 $3$ 个整数 $u$,$v$,$w$,每两个整数之间用一个空格隔开,表示从城市 $u$ 到城市 $v$ 有一条长为 $w$ 的道路。数据保证输入的是一棵树,且根节点编号为 $1$。

接下来一行一个整数 $m$,表示军队个数。

接下来一行 $m$ 个整数,每两个整数之间用一个空格隔开,分别表示这 $m$ 个军队所驻扎的城市的编号。

输出格式

共一行,包含一个整数,表示控制疫情所需要的最少时间。如果无法控制疫情则输出 $-1$。

样例数据

输入

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

输出

3

说明

第一支军队在 $2$ 号点设立检查点,第二支军队从 $2$ 号点移动到 $3$ 号点设立检查点,所需时间为 $3$ 个小时。

数据范围

保证军队不会驻扎在首都。

对于 $20\%$ 的数据,$2 \le n \le 10$;

对于 $40\%$ 的数据,$2 \le n \le 50$,$0 < w < 10^5$;

对于 $6\0%$ 的数据,$2 \le n \le 1000$,$0 < w < 10^6$;

对于 $80\%$ 的数据,$2 \le n \le 10{,}000$;

对于 $100\%$ 的数据,$2 \le m \le n \le 50{,}000$,$0 < w < 10^9$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.