Telecommunication Costs
A telecommunication company is trying to attract a new customer. The customer is interested in leasing optical fibers that connect cities, denoted as , , , . The company needs to submit a proposal with the cost for the direct connection of each pair of cities and , , , where Based on the proposal, the customer will select a spanning tree that covers all cities and ensures the minimum total connection cost.
The company has significant reasons to influence the customer's choice, leading them to choose a specific spanning tree for connecting the cities. Given the cost of the connections included in , the company desires to adjust the cost of the remaining connections so that emerges as the unique minimum spanning tree covering all cities. However, to avoid appearing too expensive to the customer, the company wants the proposed cost for the remaining connections to be calculated so that the total sum of the proposed costs for all city pairs is minimized.
Problem:
Develop a program in one of the IOI languages which, after reading the costs of the connections included in the spanning tree , calculates the minimum possible total proposed cost in the company's proposal so that is the unique minimum spanning tree.
Input Files:
The input files, named telecom.in, are text files with the following structure: The first line contains an integer , the number of cities. Cities are numbered from to . Each of the next lines contains three positive integers , , and , separated in pairs by a single space. These indicate that city is directly connected to city in the selected tree with a cost of . It holds true that , , , and .
Output Files:
The output files named telecom.out are text files with the following structure: They contain only one line with a single integer, the minimum total proposed cost for all city pairs, so that the selected tree is the unique minimum spanning tree for cities , , , based on the proposed costs.
Note: For large values of , the minimum total cost (and possibly some of the intermediate results needed for its calculation) may exceed .
Examples of Input - Output Files:
1st
STDIN (telecom.in)
3
1 2 4
2 3 7
STDOUT (telecom.out)
19
Explanation of the first example:
The selected tree has direct connections between cities and , with a cost of , and between cities and , with a cost of . For this to be the unique minimum spanning tree among cities , , and , the proposed cost for connecting cities and must be at least . Therefore, the minimum total proposed cost is equal to .
2nd
STDIN (telecom.in)
4
1 2 1
1 3 1
1 4 2
STDOUT (telecom.out)
12
Explanation of the second example:
The selected tree has direct connections between cities and , with a cost of , between cities and , with a cost of , and between cities and , with a cost of . For this to be the unique minimum spanning tree among cities , , , and , the proposed cost for connecting cities and must be at least , for connecting cities and , the cost must be at least , and for connecting cities and , the cost must be at least . Therefore, the minimum total proposed cost is equal to .
Formatting: In the output, each line terminates with a newline
character.
Maximum execution time: 1 sec.
Maximum available memory: 64 MB.
Comments