Under construction

Still being built. Things may change

Minimum spanning tree and queries

Time limit5sMemory limit256 MB

Summary
Given a weighted directed graph G, find the MST weight of G tensor Path_i for each i from 2 to Q+1, where Path_i is a chain of i vertices.
Level

Hard9 of 10

Topics
Minimum spanning tree, Graph, Union-find, Dynamic programming
Solved
No attempts yet

문제

PathiPath_i is a directed graph with ii vertices, and for every 1≤j<i1 \le j < i there is an edge j→j+1j \rightarrow j + 1.

The tensor product G1×G2G_1 \times G_2 of a weighted directed graph G1G_1 and a directed graph G2G_2 is defined as follows.

  • The vertex set is {(u,v)∣u∈G1,v∈G2}\{(u, v) \mid u \in G_1, v \in G_2\}. It has ∣G1∣×∣G2∣|G_1| \times |G_2| vertices.
  • Two vertices (u1,v1)(u_1, v_1) and (u2,v2)(u_2, v_2) are joined by an undirected edge if G1G_1 has an edge u1→u2u_1 \rightarrow u_2 and G2G_2 has an edge v1→v2v_1 \rightarrow v_2.
  • The weight of that edge equals the weight of the edge u1→u2u_1 \rightarrow u_2 in G1G_1.

You are given a directed graph GG with NN vertices and MM edges, and each edge has a positive integer weight. Consider G×PathiG \times Path_i. It is undirected, and each of its edges has a positive integer weight. For every 2≤i≤Q+12 \le i \le Q + 1, output the total weight of a minimum spanning tree of G×PathiG \times Path_i. The input guarantees that every G×PathiG \times Path_i asked in a query has a spanning tree.

입력

The first line contains three integers NN, QQ, and MM (1≤N,Q≤1000001 \le N, Q \le 100000, 1≤M≤2000001 \le M \le 200000).

Each of the next MM lines contains three integers uiu_i, viv_i, and wiw_i, meaning there is an edge ui→viu_i \rightarrow v_i with weight wiw_i (1≤ui,vi≤N1 \le u_i, v_i \le N, 1≤wi≤301 \le w_i \le 30).

출력

Print QQ lines. The ii-th line contains the answer for G×Pathi+1G \times Path_{i+1}.

Examples3

  1. Example 1

    Input
    4 4 8
    3 4 1
    1 1 3
    1 3 2
    4 2 4
    4 4 2
    2 2 3
    1 2 2
    1 4 3
    
    Expected output
    16
    23
    30
    37
    
  2. Example 2

    Input
    4 4 8
    3 4 12
    1 1 20
    1 3 22
    4 2 12
    4 4 2
    2 2 2
    1 2 2
    1 4 2
    
    Expected output
    62
    80
    98
    116
    
  3. Example 3

    Input
    6 6 15
    1 2 1
    1 3 1
    3 4 1
    2 4 1
    6 3 2
    6 5 2
    3 5 2
    2 3 2
    4 3 2
    6 4 2
    5 4 2
    4 6 2
    6 6 2
    5 5 3
    5 1 3
    
    Expected output
    19
    28
    37
    46
    55
    64