QOJ.ac

QOJ

Time Limit: 3.0 s Memory Limit: 512 MB Total points: 100

#18178. 门票

Statistics

Berland 有 $n$ 个城市,编号为 $1$ 到 $n$。其中 $1$ 号城市是 Berland 的首都。

城市之间由 $n - 1$ 条铁路线连接,第 $i$ 条铁路线连接城市 $a_i$ 和 $b_i$。Berland 的铁路系统保证任意两个城市之间都是连通的(可能需要乘坐多次列车)。

这里有 $m$ 种车票:第 $i$ 种车票可以在城市 $v_i$ 以 $w_i$ Berland 币的价格购买,允许持票者从 $v_i$ 旅行到任意满足 $v_i$ 与 $x$ 之间距离小于或等于 $k_i$ 的城市 $x$。距离定义为旅途中乘坐的列车次数(即两点间唯一简单路径上的边数)。

你的任务是求出从某些城市出发到达首都的最小花费。

输入格式

第一行包含三个整数 $n, m, q$ ($1 \le n \le 10^5, 0 \le m \le 10^5, 1 \le q \le 10^5$),分别表示 Berland 的城市数量、车票种类数和询问次数。

接下来的 $n - 1$ 行,每行包含两个整数 $a_i$ 和 $b_i$ ($1 \le a_i, b_i \le n$),表示第 $i$ 条铁路线连接的两个城市。

接下来的 $m$ 行描述车票种类:第 $i$ 行包含三个整数 $v_i$ ($1 \le v_i \le n$) —— 可以购买并使用该车票的城市,$k_i$ ($1 \le k_i \le n - 1$) —— 使用该车票能旅行的最大距离,$w_i$ ($0 \le w_i \le 10^9$) —— 该车票的价格。

接下来的 $q$ 行描述询问:第 $i$ 行包含一个整数 $q_i$ ($1 \le q_i \le n$) —— 想要计算到达首都最小花费的起点城市。

输出格式

对于每个询问,输出一行一个整数,表示从该城市到达首都所需的最小花费。如果使用这些车票无法到达首都,则输出 Impossible

样例

输入样例 1

5 4 5
1 2
2 3
3 4
3 5
5 2 10
3 1 40
4 3 1
2 1 100
1
2
3
4
5

输出样例 1

0
100
41
1
11

输入样例 2

2 0 2
1 2
1
2

输出样例 2

0
Impossible

说明

在第一个样例中,要从城市 5 到达首都,我们需要执行以下操作:

  • 在城市 5 购买一张价格为 10 的 1 号车票,并旅行到城市 4。
  • 在城市 4 购买一张价格为 1 的 3 号车票,并旅行到首都城市 1。

注意,也可以使用 1 号车票旅行到城市 3 和城市 2(5 到 3 的距离为 1,而 5 到 2 的距离为 2,均小于或等于 2),然后使用 2 号或 4 号车票到达首都,但这样到达首都的花费会高得多。

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.