QOJ.ac

QOJ

Limite de temps : 2 s Limite de mémoire : 512 MB Points totaux : 100

#8342. Spanning Tree

Statistiques

Little D has an undirected weighted graph $G$ with $n$ vertices and $m$ edges, where the $i$-th edge connects $u_i$ and $v_i$ with weight $w_i$.

Little D also has two sequences $x$ and $y$ of length $k$, and a set $S \subseteq [0, n)$.

Little D constructs a new graph $H$ with a total of $nk$ vertices, where each vertex is represented by a pair $(a, b)$ with $a \in [0, k)$ and $b \in [0, n)$.

The edge set of the new graph $H$ consists exactly of the following types of edges: 1. For $a \in [0, k)$ and $i \in [0, m)$, there exists an edge connecting $(a, u_i)$ and $(a, v_i)$ with weight $w_i + y_a$. 2. For $a \in [0, k-1)$ and $i \in S$, there exists an edge connecting $(a, i)$ and $(a+1, i)$ with weight $x_a$. 3. For $i \in S$, there exists an edge connecting $(k-1, i)$ and $(0, i)$ with weight $x_{k-1}$.

Find the weight of the minimum spanning tree of graph $H$.

Input

The first line contains two integers $n$ and $m$.

The next $m$ lines each contain three integers $u_i, v_i, w_i$.

The next line contains an integer $k$.

The next $k$ lines each contain two integers $x_i, y_i$.

The next line contains an integer $r$ representing the size of set $S$.

The next $r$ lines each contain an integer representing an element of set $S$.

Output

A single integer representing the weight of the minimum spanning tree.

Examples

Input 1

2 1
0 1 3
3
6 1
4 2
5 3
1
0

Output 1

24

Input 2

3 3
0 1 7
1 2 8
2 0 5
4
8 1
5 1
9 3
7 3
2
0
1

Output 2

76

Constraints

Subtask ID $n, m, k \le$ Special Property Score
1 $1000$ None 12
2 $10^5$ $x_i = 0$ 14
3 $10^5$ $y_i = 0$ 19
4 $10^5$ $m = n - 1, r = n$ 23
5 $10^5$ None 32

For all data, $1 \le n, m \le 10^5$, $2 \le k \le 10^5$, $0 \le u_i, v_i < n$, $0 \le w_i, x_i, y_i \le 10^8$, $1 \le r \le n$, $0 \le s_i < n$, $s_i$ are distinct, and $H$ is guaranteed to be connected.

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.