[Usaco2007 Jan] telephone lines build telephone lines [Dual answer + shortest circuit]

Source: Internet
Author: User
Description

Farmer John planned to direct the telephone line to his farm, but the telecommunications company did not plan to provide him with free services. Therefore, FJ must pay a certain fee to the telecommunications company. There are N (1 <= n <= 1,000) roots distributed around the farm of FJ .. N sequential number of discarded telephone lines, any two telephone lines are not connected to the telephone line. A total of P (1 <= P <= 10,000) can be used to pull telephone lines between telephone lines, and others cannot be connected due to the distance. The two ends of the I-th telephone pole are A_ I and B _ I, respectively, and the distance between them is L_ I (1 <= L_ I <= 1,000,000 ). Ensure that each pair of {a_ I, B _ I} appears only once at most. The telephone pole numbered 1 has been connected to the national telephone network, and all the telephone lines on the farm are connected to the telephone pole numbered n. That is to say, the task of Fj is only to find a path to connect telephone lines 1 and N. Other telephone lines do not have to be connected to the telephone network. After negotiation, the telecommunications company eventually agreed to connect K (0 <= k <n) to the telephone pole specified by FJ for free. For other telephone lines, FJ will pay for them, equal to the length of the longest telephone line (each telephone line is associated with only one pair of telephone lines ). If the number of telephone lines to be connected does not exceed K pairs, the total cost of Fj is 0. Calculate the minimum cost of Fj over the telephone line.

Input

* Row 1st: Three integers separated by spaces: N, P, and K

* Row 2nd. p + 1: I + 1 Act 3 integers separated by spaces: a_ I, B _ I, L_ I

Output

* Row 1st: output an integer, which is the minimum expenditure of Fj on this project. If the task cannot be completed, output-1

Sample input5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

Input description:

A total of five discarded telephone lines. Telephone Line 1 cannot be directly connected to telephone lines 4 or 5. Phone number
Pole 5 cannot be directly connected to telephone lines 1 or 3. Telephone lines can be used between all other telephone lines. China Telecom
The company can connect one pair of telephone lines to FJ for free.

Sample output4

Output description:

FJ selects the following link scheme: 1-> 3; 3-> 2; 2-> 5, which is required between the three telephone lines
The telephone lines are 4, 3, and 9 respectively. FJ asked the telecommunications company to provide the telephone line with a length of 9, so,
The maximum length of the telephone line he needs to purchase is 4.

 

Ideas:

Similar to the minimum maximum, the longest two-digit telephone line is required;

The basis for determining whether the mid is feasible is,

There is a path where the number of edges greater than the mid value cannot exceed K;

Here we need to use spfa. Let's set the edge weight greater than mid to 1, and the edge weight smaller than mid to 0, and find the shortest short circuit again. (This is the magic of the shortest circuit !)

Code attached:

 

[Usaco2007 Jan] telephone lines build telephone lines [Dual answer + shortest circuit]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.