Conqueror
InterviewTime limit2sMemory limit256 MB
Conquer all cities from city 1, where the k-th city you take costs the edge cost plus (k-1)*t, and minimize the total.
- Level
Medium7 of 10
- Topics
- Minimum spanning tree, Greedy, Graph, Sorting
- Solved
- No attempts yet
Problem
The land of Seogang consists of cities and roads. Every road is bidirectional, and between any two cities there is a path that follows roads. Each road has a cost you pay to use it. The cities are numbered from to , and Park Geon, the ruler of city , wants to conquer every city.
At the start he holds city only. To conquer a city , he must already hold at least one of the cities joined to by a road. Once he picks such a city , conquering costs the cost of the road between and . Park Geon attempts one city at a time and always succeeds. Each time a city falls, the remaining cities go on alert and the cost of every road rises by . A city that has been conquered is never conquered again.
Find the minimum total cost for Park Geon to conquer every city.
Input
The first line contains the number of cities , the number of roads , and the amount by which every road cost rises after each conquest. is a natural number at most , is a natural number at most , and is a natural number at most .
Each of the next lines contains three natural numbers , , describing a road: there is a road of cost between city and city . and are distinct natural numbers at most , and is a natural number at most . More than one road may join the same pair of cities.
Output
Print the minimum total cost of conquering every city.
Hint
In the first example, city , which is joined to city , falls first. Conquering it costs . After city falls, every road cost rises by . The cities held are and .
City is joined to city , which is already held, so it can be conquered from city at the cost of the road between cities and , which is now . The cities held are , , and .
City is also joined to city , so it can be conquered too. Conquering it costs the road between cities and , which is now . Every city has now fallen.
The total is .