Bridges
Time limit2sMemory limit64 MB
Choose k edges of a weighted tree to convert to a faster speed so that the sum of travel times over all pairs is minimized, breaking ties lexicographically.
- Level
Hard8 of 10
- Topics
- Tree, Greedy, Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
Long ago, a country lay in the delta of a distant river. The country had islands, with one town on each island. The towns were connected by roads so that there was exactly one route between every pair of towns (possibly passing through some intermediate towns). In other words, the towns and roads form a tree.
Every road crossed the river at a ford. Because bridges were unknown, crossing a ford was uncomfortable and could only be done on horseback.
When bridge-building technology was discovered, the king decided to replace some fords with bridges so that those roads would be easier to travel: a road with a bridge can be crossed by a carriage. The country was poor, so only bridges could be built.
You must choose the roads to bridge so that the total travel time over all pairs of towns is as small as possible. A road without a bridge is traveled by horse at speed ; a road with a bridge is traveled by carriage at speed (speeds are in meters per second). Traversing a single road of length takes by horse or by carriage. The travel time between two towns is the sum of the travel times of the roads on the unique route between them, and the total travel time is the sum of that quantity over all unordered pairs of distinct towns.
Input
The first line contains four integers , , , and : the number of towns, the number of bridges to build (), the horse speed, and the carriage speed in meters per second ().
Each of the next lines contains three integers , , and : the two towns joined by road and that road's length in meters (). Towns are numbered from to , and roads are numbered from to in the order they are given.
Output
Output the numbers of the roads on which to build bridges so that the total travel time is minimized. Print them on one line in increasing order, separated by single spaces.
If several plans achieve the minimum total travel time, output the lexicographically smallest one: among all optimal plans, write each plan's chosen road numbers in increasing order and print the sequence that is lexicographically smallest.
Hint

The figure illustrates an example configuration.