![]() Figure 1. The scheme depicts all towns (nodes) and main roads (edges) in the region. District towns have identifiers 1, 2 and 3. The colored backgrounds show towns in their districts. Blue numbers along the edges are costs for snow removal on a given road. An optimal solution is indicated by purple and dashed red edges. Purple roads are made operational during the first phase. They connect towns within districts. Dashed red roads are made operational during the second phase. They ensure the connection for any pair of towns throughout the region. The total cost of implementing this plan is 52. |
The topology of the main roads between the towns in the region and the costs for clearing them of snow are given. It is also known which towns are district towns. Find an optimal snow removal plan that follows the defined two-phase strategy and minimizes the sum of costs paid for clearing selected main roads.
The first input line contains three integers T, D, R, separated by spaces. T is the number of towns, D
is the number of district towns, R is the number of main roads. Towns are numbered from 1 to T, district towns are numbered from
1 to D. These numbers correspond to town id's.
R input lines representing the main roads follow. Each of them contains three integers T1,
T2, C, separated by spaces, that represent the main road between towns T1 and T2.
Integer C is the cost for clearing this road of snow. The roads are listed in random order.
It holds T ≤ 2.5 × 105, D ≤ 2000, R ≤ 4.5 × 105. The cost for clearing
any main road of snow is positive and not greater than 250.
The output contains one line with one integer which equals the minimum total cost for clearing main roads of snow in both phases.
Example 1Input6 2 7 2 4 3 3 1 5 2 1 4 5 3 5 6 4 3 3 4 4 6 5 2Output 18 |
Example 2Input24 3 40 12 2 3 7 13 2 7 8 3 14 20 3 3 22 1 21 4 1 23 24 3 12 18 4 10 2 4 9 10 3 18 24 4 16 10 3 15 9 3 16 9 1 14 8 1 2 5 3 9 8 3 15 14 3 3 15 1 13 14 2 4 5 4 13 19 1 |
The public data set is intended for easier debugging and approximate program correctness checking. The public data set is stored also in the upload system and each time a student submits a solution it is run on the public dataset and the program output to stdout and stderr is available to him/her.
Link to public data set