Tolled Road Segments
Time limit4sMemory limit1024 MB
Find the largest total toll on the main highway segments so that no driver prefers a side-road detour.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path
- Solved
- No attempts yet
Problem
The government plans to charge a toll on some segments of the main highway between Tallinn and Tartu. Drivers try to avoid tolled segments whenever they can, so they take a side road around a toll if it is cheaper. When the costs are equal, a driver always prefers the main highway.
If cars start driving through local villages, residents will be very angry, and the government could fall. The government wants the largest possible toll revenue while avoiding that anger.
Find the largest total amount the government can charge on the segments of the main highway, so that a driver who starts and ends at any point on the main highway finds it optimal to drive only along the main highway.
It is known that the main highway is already optimal: no route between its endpoints that uses a side road is cheaper than the route along the main highway. It is also known that every single main highway segment can be avoided using other road segments, so no segment's price can be raised without limit.
Input
The first line contains four integers , , , and , where:
- is the fuel cost per kilometer in cents ().
- is the number of junctions in the road network (). The junctions are numbered .
- is the number of road segments between the junctions ().
- is the number of segments on the main highway ().
Each of the next lines contains three integers , , and . They mean that junctions and are connected by a road segment of length kilometers (). The main highway passes junctions in increasing order of their numbers, and its segments are the first ones listed in the input, in the order they appear on the highway.
Output
Print one integer to the text file: the total price of all taxed segments.