Minimum Cost Flow
Time limit3sMemory limit512 MB
Given a spanning tree and extra edges, with a one-pipe discount available, find the minimum number of edge swaps to reach a minimum-cost spanning tree.
- Level
Hard8 of 10
- Topics
- Minimum spanning tree, Graph, Union-find, Greedy
- Solved
- No attempts yet
Problem
The city of Watermoo has buildings numbered through , and pipes that connect pairs of buildings. Because of an oversight in urban planning, building is the only sewage treatment plant in the city.
Each pipe is either active or inactive. The set of active pipes is a valid plan if building is connected, directly or indirectly, to every other building using active pipes. A pipe directly connects a pair of buildings. Buildings and are indirectly connected if is directly or indirectly connected to and is directly or indirectly connected to .
The municipal government of Watermoo runs a valid plan of pipes today, and it considers that plan too expensive. Each pipe has a monthly maintenance fee that the city pays while the pipe is active, and the total cost of a plan is the sum of the maintenance fees of its active pipes. An inactive pipe costs nothing.
Researchers at the University of Watermoo built an experimental pipe enhancer that you can use on one pipe of your choice. Using it reduces that pipe's cost from to , where is the strength of the enhancer.
The city wants the cost of the plan to be minimum, and it wants the work done quickly. Each day you can activate one pipe and deactivate another pipe. How many days do you need so that the set of active pipes is a valid plan whose cost is minimum over all valid plans and all choices of the enhanced pipe?
The plan may be invalid while you work, but it has to be a valid plan at the end.
Input
The first line contains the integers , , and . (, , )
Each of the next lines contains three integers , , and , which means that a pipe connects building and building and costs per month while it is active. (, )
The first of these lines are the valid plan the city is using now.
At most one pipe connects any two buildings, and no pipe connects a building to itself.
Output
Print one integer on a single line, the minimum number of days needed to finish the task. If the plan the city is using now is already optimal, print .
Hint
In the first example , so it does not matter which pipe you use the enhancer on, because no maintenance fee changes. On the first day, deactivate the pipe between building and building and activate the pipe between building and building .
One way to finish the second example in the minimum number of days is this. First use the enhancer on the pipe between building and building to reduce its cost to . On the first day, replace the pipe between building and building with the pipe between building and building . On the second day, replace the pipe between building and building with the pipe between building and building . The cost of the optimal plan is . Using the enhancer on the pipe between building and building , or on the pipe between building and building , is not allowed in any solution: that pipe would have a maintenance fee of , the optimal plan would then cost , and a cost of is already reachable.
In the third example the plan the city is using now is already optimal. Watch out for integer overflow when you implement your solution.