#abc369e. E - Sightseeing Tour

E - Sightseeing Tour

Score : 450450 points

问题陈述

NN 个岛屿和 MM 个双向桥连接两个岛屿。岛屿和桥分别编号为 11, 22, \ldots, NN11, 22, \ldots, MM。 桥 ii 连接岛屿 UiU_iViV_i,并且无论哪个方向穿越它都需要 TiT_i 时间。 没有桥连接一个岛屿到它自己,但两个岛屿之间可能由多于一座桥直接连接。 可以使用一些桥在任意两个岛屿之间旅行。

你给出了 QQ 个查询,因此回答每一个。第 ii 个查询如下:

你给出了 KiK_i 个不同的桥:桥 Bi,1,Bi,2,,Bi,KiB_{i,1}, B_{i,2}, \ldots, B_{i,K_i}。 使用这些桥至少一次,找到从岛屿 11 到岛屿 NN 旅行所需的最短时间。 只考虑过桥所花费的时间。 你可以以任何顺序和任何方向穿越给定的桥。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There are NN islands and MM bidirectional bridges connecting two islands. The islands and bridges are numbered 11, 22, \ldots, NN and 11, 22, \ldots, MM, respectively.
Bridge ii connects islands UiU_i and ViV_i, and the time it takes to cross it in either direction is TiT_i.
No bridge connects an island to itself, but it is possible for two islands to be directly connected by more than one bridge.
One can travel between any two islands using some bridges.

You are given QQ queries, so answer each of them. The ii-th query is as follows:

You are given KiK_i distinct bridges: bridges Bi,1,Bi,2,,Bi,KiB_{i,1}, B_{i,2}, \ldots, B_{i,K_i}.
Find the minimum time required to travel from island 11 to island NN using each of these bridges at least once.
Only consider the time spent crossing bridges.
You can cross the given bridges in any order and in any direction.

Constraints

  • 2N4002 \leq N \leq 400
  • N1M2×105N-1 \leq M \leq 2 \times 10^5
  • 1Ui<ViN1 \leq U_i < V_i \leq N
  • 1Ti1091 \leq T_i \leq 10^9
  • 1Q30001 \leq Q \leq 3000
  • 1Ki51 \leq K_i \leq 5
  • $1 \leq B_{i,1} < B_{i,2} < \cdots < B_{i,K_i} \leq M$
  • All input values are integers.
  • It is possible to travel between any two islands using some bridges.

Input

The input is given from Standard Input in the following format:

NN MM

U1U_1 V1V_1 T1T_1

U2U_2 V2V_2 T2T_2

\vdots

UMU_M VMV_M TMT_M

QQ

K1K_1

B1,1B_{1,1} B1,2B_{1,2} \cdots B1,K1B_{1,{K_1}}

K2K_2

B2,1B_{2,1} B2,2B_{2,2} \cdots B2,K2B_{2,{K_2}}

\vdots

KQK_Q

BQ,1B_{Q,1} BQ,2B_{Q,2} \cdots BQ,KQB_{Q,{K_Q}}

Output

Print QQ lines. The ii-th line (1iQ1 \leq i \leq Q) should contain the answer to the ii-th query as an integer.

Sample Input 1

3 5
1 2 10
1 3 20
1 3 30
2 3 15
2 3 25
2
1
1
2
3 5

Sample Output 1

25
70

For the first query, we need to find the minimum time to travel from island 11 to island 33 while using bridge 11. The minimum time is achieved by using bridge 11 to move from island 11 to island 22, then using bridge 44 to move from island 22 to island 33. The time taken is 10+15=2510 + 15 = 25. Hence, print 2525 on the first line.

For the second query, we need to find the minimum time to travel from island 11 to island 33 while using both bridges 33 and 55. The minimum time is achieved by using bridge 33 to move from island 11 to island 33, then using bridge 55 to move to island 22, and finally using bridge 44 to return to island 33. The time taken is 30+25+15=7030 + 25 + 15 = 70. Hence, print 7070 on the second line.

Sample Input 2

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

Sample Output 2

5
3

For each query, you can cross the specified bridges in either direction.

Sample Input 3

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

Sample Output 3

4000000000

Beware that the answer may not fit in a 3232-bit integer.