#USACOC2213B. Connecting Two Barns
Connecting Two Barns
Description
Farmer John's farm consists of a set of fields,conveniently numbered . Between these fields are bi-directedpaths, each connecting a pair of fields.
The farm contains two barns, one in field 1 and the other in field . FarmerJohn would like to ensure that there is a way to walk between the two barnsalong some series of paths. He is willing to build up to two new paths toaccomplish this goal. Due to the way the fields are situated, the cost ofbuilding a new path between fields and is .
Please help Farmer John determine the minimum cost needed such that barns and become reachable from each-other.
Input Format
Each input test case contains sub-cases, all of which mustbe solved correctly to solve the input case.
The first line of input contains , after which sub-test cases follow.
Each sub-test case starts with two integers, and . Next, linesfollow, each one containing two integers and , indicating a path betweentwo different fields and . It is guaranteed that there is at most onepath between any two fields, and that the sum of over all sub-test casesis at most .
Output Format
Output lines. The th line should contain a single integer giving theminimum cost for the th sub-test case.
2
5 2
1 2
4 5
5 3
1 2
2 3
4 5
2
5 2
1 2
4 5
5 3
1 2
2 3
4 5
Scoring
- Test case 2 satisfies .
- Test cases 3-5 satisfy .
- Test cases 6-10 satisfy no additional constraints.
Problem Credit
Problem credits: Nick Wu