QOJ.ac

QOJ

実行時間制限: 0.5 s メモリ制限: 32 MB 満点: 100

#16018. 旅程

統計

Byteland 有 $n$ 个城市(编号为 $1$ 到 $n$),由双向道路连接。Byteland 的国王不是很慷慨,所以只有 $n - 1$ 条道路,但它们连接城市的方式使得从每个城市都可以到达任何其他城市。

一天,旅行者 Byterider 到达了 $k$ 号城市。他计划进行一次旅行,从城市 $k$ 出发,并在途中访问城市 $m_1, m_2, \dots, m_j$(不一定按此顺序)—— 这些 $m_i$ 互不相同,且都与 $k$ 不同。像每个旅行者一样,Byterider 的资金有限,因此他希望使用尽可能短的路径(从城市 $k$ 开始)访问他计划访问的所有城市。路径是一条道路或一系列道路,其中每条相邻的道路都始于前一条道路结束的城市。帮助 Byterider 确定他旅行的最短路径长度。

任务

编写一个程序:

  • 从标准输入读取:
    • 连接 Byteland 城市的道路描述,
    • Byterider 到达的城市编号,
    • Byterider 想要访问的城市列表。
  • 确定 Byterider 旅行的最短路径长度。
  • 将结果写入标准输出。

输入格式

第一行包含两个整数 $n$ 和 $k$,用单个空格分隔($2 \le n \le 50\,000$,$1 \le k \le n$),$n$ 是 Byteland 的城市数量,$k$ 是 Byterider 路径上的起点城市编号。

接下来的 $n - 1$ 行每行包含 Byteland 中一条道路的描述。第 $i + 1$ 行(对于 $1 \le i \le n - 1$)包含三个整数 $a_i$、$b_i$ 和 $d_i$,用单个空格分隔($1 \le a_i, b_i \le n$,$1 \le d_i \le 1\,000$),$a_i$ 和 $b_i$ 是该道路连接的城市,$d_i$ 是道路的长度。

第 $n + 1$ 行包含一个整数 $j$ —— Byterider 想要访问的城市数量($1 \le j \le n - 1$)。

下一行包含 $j$ 个不同的整数 $m_i$,用单个空格分隔 —— Byterider 想要访问的城市编号($1 \le m_i \le n$,$m_i \ne k$)。

输出格式

标准输出的第一行也是唯一一行应包含恰好一个整数:Byterider 旅行的最短路径长度。

样例

输入样例 1

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

输出样例 1

5

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.