Under construction

Still being built. Things may change

Daedong Yeojido Roads

Interview

Time limit1sMemory limit1024 MB

Summary
Build a minimum-cost road network that connects all places, breaking cost ties by road-type priority, and report the count and cost of each road type.
Level

Medium5 of 10

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

Problem

Sejong is looking at Daedong Yeojido, a map from the Joseon era. He wonders how much it would cost to build roads that connect every region, including Hanyang.

Three kinds of roads can be built: walking-only roads, horse-only roads, and carriage-only roads.

Sejong wants to build every road that can connect two regions, but the royal treasury is short on funds. He will build roads so that every region can be reached from every other region at the minimum total cost. If several sets of roads reach that minimum cost, he picks the set with the most first-priority roads. If that still ties, he picks the set with the most second-priority roads.

Help Sejong find the minimum cost, and report how many roads of each type are built along with their total cost.

Input

The first line has the number of places NN and the number of roads MM. (2≤N≤10 000(2 \le N \le 10\,000, N−1≤M≤200 000)N-1 \le M \le 200\,000)

The second line has three different road types pip_i in priority order, highest first. Each value is 00 for walking-only, 11 for horse-only, or 22 for carriage-only. (0≤pi≤2)(0 \le p_i \le 2)

Each of the next MM lines has four values uiu_i, viv_i, wiw_i, kik_i. uiu_i and viv_i are place numbers. (ui≠vi(u_i \ne v_i, 1≤ui,vi≤N)1 \le u_i, v_i \le N). wiw_i is the cost to build the road. (1≤wi≤10 000)(1 \le w_i \le 10\,000). kik_i is the road type, where 00 is walking, 11 is horse, and 22 is carriage. (0≤ki≤2)(0 \le k_i \le 2)

The input guarantees that all places can be connected.

Output

On the first line, print the minimum cost to connect all NN places.

On the second line, print the number of walking-only roads built and their total cost, separated by a space.

On the third line, print the number of horse-only roads built and their total cost, separated by a space.

On the fourth line, print the number of carriage-only roads built and their total cost, separated by a space.

Examples2

  1. Example 1

    Input
    5 4
    0 1 2
    1 2 1 0
    2 3 1 1
    3 4 3 2
    4 5 2 0
    
    Expected output
    7
    2 3
    1 1
    1 3
    
  2. Example 2

    Input
    5 30
    0 1 2
    1 3 3 0
    1 3 16 1
    1 3 10 2
    2 3 16 0
    2 3 15 1
    2 3 15 2
    5 4 6 0
    5 4 13 1
    5 4 9 2
    3 4 13 0
    3 4 3 1
    3 4 4 2
    5 2 13 0
    5 2 16 1
    5 2 12 2
    5 3 7 0
    5 3 16 1
    5 3 1000 2
    2 5 7 0
    2 5 1 1
    2 5 13 2
    3 2 1 0
    3 2 3 1
    3 2 4 2
    1 3 16 0
    1 3 1 1
    1 3 8 2
    2 4 3 0
    2 4 7 1
    2 4 1 2
    
    Expected output
    4
    1 1
    2 2
    1 1