#P15455. [JOI 2026 SemiFinal] 新たな橋 / New Bridge

[JOI 2026 SemiFinal] 新たな橋 / New Bridge

Problem Description

The country of JOI consists of NN islands, numbered from 11 to NN. Currently, there are no bridges connecting the islands, so life is very inconvenient for the residents.

Therefore, as a minister of the country of JOI, you decide to build new bridges as a national project. There are MM bridge construction plans. The jj-th plan (1≤j≤M1 \le j \le M) is to build a two-way bridge between islands AjA_j and BjB_j with cost CjC_j. It is guaranteed that C1,C2,…,CMC_1, C_2, \dots, C_M are all distinct. It is also guaranteed that if all construction plans are carried out, then all islands will be mutually reachable via some bridges.

Because the budget of the country of JOI is limited, you decide to carry out the national project in the following way:

  1. Choose one island ss from the NN islands, and make it the capital.
  2. Perform the following operation N−1N - 1 times:
    • Before each operation, call the islands that are reachable from the capital via some bridges near islands, and call the other islands far islands. Among the construction plans whose one endpoint is a near island and the other endpoint is a far island, choose the one with the smallest cost and carry it out.
  3. After performing the operation N−1N - 1 times, the national project ends.

From the constraints satisfied by the construction plans, the following facts can be proved:

  • In each operation, there is always at least one construction plan that can be chosen. Also, the construction plan that is carried out is uniquely determined.
  • When the project ends, all islands are mutually reachable via some bridges.

Rin, who is considering moving to the country of JOI, decides to compute the “inconvenience” of each island in the following way in order to decide which island to live on. The inconvenience of island ii (1≤i≤N1 \le i \le N) is defined as follows:

  • Let Ds,iD_{s,i} be the number of construction plans carried out until island ii becomes reachable from the capital, when the national project is carried out with island ss (1≤s≤N1 \le s \le N) as the capital. Here, when s=is = i, Ds,iD_{s,i} is 00.
  • The inconvenience of island ii is the sum of Ds,iD_{s,i} over all 1≤s≤N1 \le s \le N.

Rin wants to compute the inconvenience of QQ candidate islands X1,X2,…,XQX_1, X_2, \dots, X_Q that she is considering. Given the construction plans and the candidate islands, write a program to find the inconvenience of these islands.

Input Format

Input is given from standard input in the following format:

N M QN\ M\ Q
A1 B1 C1A_1\ B_1\ C_1
A2 B2 C2A_2\ B_2\ C_2
⋮\vdots
AM BM CMA_M\ B_M\ C_M
X1X_1
X2X_2
⋮\vdots
XQX_Q

Output Format

Output QQ lines. In the kk-th line, output the inconvenience of island XkX_k (1≤k≤Q1 \le k \le Q).

4 5 2
1 3 2
1 4 4
2 3 1
2 4 5
3 4 3
1
3
7
3
5 4 5
1 2 3
2 3 1
3 4 4
4 5 2
1
2
3
4
5
12
8
7
10
13
10 20 1
1 2 808642746
1 3 990324141
1 4 69919024
1 5 794837863
3 6 84751636
1 7 491226767
3 8 314795065
1 9 347506932
1 10 709806198
2 3 103026123
9 10 270175384
4 8 133038160
4 10 592110162
2 10 708615085
6 10 262209760
5 10 75049025
7 9 367273075
6 9 264231132
3 10 909786421
2 7 135810916
10
43

Hint

Sample Explanation 1

For example, consider the case where the national project is carried out with island 11 as the capital. Then, the construction plans will be carried out in the following order:

  1. Carry out the 11-st construction plan. Island 33 becomes newly reachable from the capital.
  2. Carry out the 33-rd construction plan. Island 22 becomes newly reachable from the capital.
  3. Carry out the 55-th construction plan. Island 44 becomes newly reachable from the capital.

Thus, $D_{1,1} = 0,\ D_{1,2} = 2,\ D_{1,3} = 1,\ D_{1,4} = 3$. Since D2,1=2, D3,1=2, D4,1=3D_{2,1} = 2,\ D_{3,1} = 2,\ D_{4,1} = 3, the inconvenience of island 11 is $D_{1,1} + D_{2,1} + D_{3,1} + D_{4,1} = 0 + 2 + 2 + 3 = 7$. Also, since D2,3=1, D3,3=0, D4,3=1D_{2,3} = 1,\ D_{3,3} = 0,\ D_{4,3} = 1, the inconvenience of island 33 is $D_{1,3} + D_{2,3} + D_{3,3} + D_{4,3} = 1 + 1 + 0 + 1 = 3$.

This sample input satisfies the constraints of subtasks 1,2,61, 2, 6.

Constraints

  • 2≤N≤300 0002 \le N \le 300\,000
  • 1≤M≤600 0001 \le M \le 600\,000
  • 1≤Q≤N1 \le Q \le N
  • 1≤Aj<Bj≤N1 \le A_j < B_j \le N(1≤j≤M1 \le j \le M)
  • If all construction plans are carried out, all islands are mutually reachable via some bridges
  • 1≤Cj≤1091 \le C_j \le 10^9(1≤j≤M1 \le j \le M)
  • C1,C2,…,CMC_1, C_2, \dots, C_M are all distinct
  • 1≤Xk≤N1 \le X_k \le N(1≤k≤Q1 \le k \le Q)
  • X1,X2,…,XQX_1, X_2, \dots, X_Q are all distinct
  • All input values are integers

Subtasks

  1. (5 points) N≤2000, M≤2000N \le 2000,\ M \le 2000
  2. (8 points) N≤2000N \le 2000
  3. (9 points) M=N−1M = N - 1, and Aj=j, Bj=j+1A_j = j,\ B_j = j + 1(1≤j≤M1 \le j \le M), Q=1Q = 1
  4. (18 points) M=N−1M = N - 1, and Aj=j, Bj=j+1A_j = j,\ B_j = j + 1(1≤j≤M1 \le j \le M)
  5. (28 points) Q=1Q = 1
  6. (32 points) No additional restrictions

Translated by DeepSeek.

Translated by ChatGPT 5