Daedong Yeojido Roads
InterviewTime limit1sMemory limit1024 MB
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 and the number of roads . ,
The second line has three different road types in priority order, highest first. Each value is for walking-only, for horse-only, or for carriage-only.
Each of the next lines has four values , , , . and are place numbers. , . is the cost to build the road. . is the road type, where is walking, is horse, and is carriage.
The input guarantees that all places can be connected.
Output
On the first line, print the minimum cost to connect all 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.