Under construction

Still being built. Things may change

Road Network Management and Queries in Sinchon

Time limit2.5sMemory limit1024 MB

Summary
Given a connected graph whose edges are colored by five schools, find the minimum spanning tree cost for each of Q new fee sets.
Level

Medium7 of 10

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

Problem

In 2025, five universities in Sinchon, Sogang University, Sookmyung Women's University, Yonsei University, Ewha Womans University, and Hongik University, decided to build and manage a road network for smooth travel across Sinchon.

Sinchon has NN vertices, and MM undirected roads connect pairs of distinct vertices. Two vertices are joined by at most one direct road, and any two vertices are connected by some path.

Each road is managed by one of the five schools. Every road needs an annual maintenance fee that depends on the school managing it. All roads managed by the same school have the same fee.

From 2125 on, the schools' budgets ran short, and the fees started to change from year to year. The five schools agreed to manage roads so that for any two vertices, a path between them exists using only managed roads. The remaining roads stay unmanaged for that year. For each year, you are given the new fee of each school. Compute the minimum total fee needed to keep the roads managed as agreed.

Input

The first line contains NN, MM, and QQ separated by spaces. (1≤N≤5×104;(1 \le N \le 5 \times 10^4; N−1≤M≤105;N-1 \le M \le 10^5; 1≤Q≤2×104)1 \le Q \le 2 \times 10^4)

Each of the next MM lines contains uiu_i, viv_i, and ziz_i separated by spaces, meaning road ii connects vertices uiu_i and viv_i and is managed by school ziz_i. (1≤ui,vi≤N;(1 \le u_i, v_i \le N; ui≠vi;u_i \neq v_i; zi∈{A,B,C,D,E})z_i \in \{A, B, C, D, E\}) A, B, C, D, and E stand for Sogang University, Sookmyung Women's University, Yonsei University, Ewha Womans University, and Hongik University, in that order.

Each of the last QQ lines, starting from line M+2M+2, contains the new fees aja_j, bjb_j, cjc_j, djd_j, and eje_j separated by spaces, in the order of the five schools above. (0≤aj,bj,cj,dj,ej≤107)(0 \le a_j, b_j, c_j, d_j, e_j \le 10^7)

Output

For each of the QQ fee changes, print on its own line the minimum total fee after the change.

Examples1

  1. Example 1

    Input
    6 9 3
    1 2 E
    1 3 C
    2 3 A
    3 6 D
    2 4 B
    2 5 D
    3 5 B
    4 5 C
    5 6 A
    10 3 4 9 8
    2 4 4 2 1
    0 2 0 2 4
    
    Expected output
    23
    11
    2