Under construction

Still being built. Things may change

Tolled Road Segments

Time limit4sMemory limit1024 MB

Summary
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 KK, RR, TT, and T_PT\_P, where:

  • KK is the fuel cost per kilometer in cents (1≤K≤1001 \le K \le 100).
  • RR is the number of junctions in the road network (2≤R≤5 0002 \le R \le 5\,000). The junctions are numbered 0…R−10 \dots R-1.
  • TT is the number of road segments between the junctions (2≤T≤15 0002 \le T \le 15\,000).
  • T_PT\_P is the number of segments on the main highway (1≤T_P≤1 0001 \le T\_P \le 1\,000).

Each of the next TT lines contains three integers R1R_1, R2R_2, and PP. They mean that junctions R1R_1 and R2R_2 are connected by a road segment of length PP kilometers (0<P≤5 0000 < P \le 5\,000). The main highway passes junctions 0…T_P0 \dots T\_P 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.

Examples1

  1. Example 1

    Input
    5 6 8 3
    0 1 2
    1 2 3
    2 3 2
    0 4 2
    1 4 2
    1 5 3
    2 5 2
    3 5 3
    
    Expected output
    15