Road Network Management and Queries in Sinchon
Time limit2.5sMemory limit1024 MB
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 vertices, and 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 , , and separated by spaces.
Each of the next lines contains , , and separated by spaces, meaning road connects vertices and and is managed by school . 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 lines, starting from line , contains the new fees , , , , and separated by spaces, in the order of the five schools above.
Output
For each of the fee changes, print on its own line the minimum total fee after the change.