| <p> | |
| Thanks to his tireless hard work, Wilson has been promoted and now gets to drive his moving company's trucks! No, he can't believe it either. | |
| </p> | |
| <p> | |
| The moving company services a region that has <strong>N</strong> towns, with <strong>M</strong> roads running amongst them. | |
| The <em>i</em>th road connects two different towns <strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>, | |
| requires <strong>G<sub>i</sub></strong> litres of gas to drive along, and can be traversed in either direction. | |
| There may be multiple roads running directly between any given pair of towns. | |
| </p> | |
| <p> | |
| Today, Wilson has been scheduled to transport <strong>K</strong> families' belongings. | |
| The <em>i</em>th family is moving from town <strong>S<sub>i</sub></strong> | |
| to a different town <strong>D<sub>i</sub></strong>. | |
| Wilson and his truck will be starting off the day at the company headquarters in town 1. | |
| For each family, he'll need to drive to their starting town by following a sequence of roads, load his truck there, | |
| and at some point later, arrive at their destination town to unload their belongings. | |
| His truck is large enough to fit at most 2 families' sets of belongings at a time, meaning that he doesn't necessarily | |
| need to deliver each load immediately after picking it up. | |
| </p> | |
| <p> | |
| However, Wilson has been instructed that the <strong>K</strong> families must be helped strictly in order. | |
| In particular, if i < j, then the <em>i</em>th family's belongings must be loaded before the <em>j</em>th family's belongings are loaded, | |
| and the <em>i</em>th family's belongings must be delivered before the <em>j</em>th family's belongings are delivered. | |
| </p> | |
| <p> | |
| Although Wilson's wages are higher than ever, he does have to pay for the truck's gas out of his own pocket, | |
| so it's in his best interest to get the job done while burning through as little of it as possible. | |
| Of course, he'll still need to be careful to follow his company's strict rules regarding the relative | |
| order of the families' loads and unloads, to avoid getting fired. | |
| That being said, it's a possibility for it to be impossible to even complete all of the requested moves, | |
| in which case Wilson will simply call it a day and stay home instead. | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of sets of families Wilson needs to move. | |
| </p> | |
| <p> | |
| For each case, there is first a line containing three space-separated integers, | |
| <strong>N</strong>, <strong>M</strong>, and <strong>K</strong>. | |
| </p> | |
| <p> | |
| Then, <strong>M</strong> lines follow, the <em>i</em>th of which contains 3 space-separated integers, | |
| <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong>, and <strong>G<sub>i</sub></strong>. | |
| </p> | |
| <p> | |
| Then, <strong>K</strong> lines follow, the <em>i</em>th of which contains 2 space-separated integers, | |
| <strong>S<sub>i</sub></strong> and <strong>D<sub>i</sub></strong>. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <em>i</em>th case, print a line containing "Case #<strong>i</strong>: " | |
| followed by the minimum amount of gas required for Wilson to validly complete his delivery schedule, or -1 if it can't be done. | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 100 <br /> | |
| 2 ≤ <strong>N</strong> ≤ 100 <br /> | |
| 1 ≤ <strong>M</strong> ≤ 5,000 <br /> | |
| 1 ≤ <strong>K</strong> ≤ 5,000 <br /> | |
| 1 ≤ <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong> ≤ <strong>N</strong>, | |
| <strong>A<sub>i</sub></strong> ≠ <strong>B<sub>i</sub></strong> <br /> | |
| 1 ≤ <strong>S<sub>i</sub></strong>, <strong>D<sub>i</sub></strong> ≤ <strong>N</strong>, | |
| <strong>S<sub>i</sub></strong> ≠ <strong>D<sub>i</sub></strong> <br /> | |
| 1 ≤ <strong>G<sub>i</sub></strong> ≤ 1,000 <br /> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| In the first case, Wilson drives to town 2, and then drives the first family's belongings back to town 1. That's 8 litres gas so far. Then Wilson drives to city 3 (11 more litres of gas), picks up the remaining belongings, and drives them all to town 2 (7 litres of gas). A grand total of 8 + 11 + 7 = 26 litres of gas. | |
| </p> | |
| <p> | |
| In the fourth case, Wilson can't reach town 4 in order to complete the 2nd and 3rd families' moves. | |
| </p> | |