Woburn Challenge 2016-17 Round 4 - Senior Division
"All right, slick Nick, you're under arrest."
"Really, for what?"
"Gee, I don't know. How about selling food without a permit,
transporting undeclared commerce across borough lines, false
advertising…"
"Here: Permit, receipt of declared commerce, and I did not falsely
advertise anything. Take care."
"You told that mouse the pawpsicle sticks were redwood!"
"That's right. Red wood. With a space in the middle. Wood that is red."
Nick Wilde has got quite the clever money-making scheme going on. Every day, he…
- Heads to an ice cream parlor, purchases a "Jumbo Pop" popsicle (unless he can hustle someone else into purchasing it for him), and melts it down into a sugary liquid.
- Transports it to Zootopia's Tundratown district, makes paw prints in a snowy field, fills them with the popsicle liquid, places a popsicle stick in each one, and waits for the resulting "Pawpsicles" to solidify.
- Sets up a sales booth outside a lemming bank just in time for the end of the workday, and sells his Pawpsicles to the lemmings as they leave the bank, collecting the discarded popsicle sticks.
- Delivers the popsicle sticks to a miniature construction site in Little Rodentia, passing it off as lumber.
By following this sequence of steps, Nick is able to pull in a handsome profit of $200 every day. And the whole operation is perfectly legal! (Well, he may be failing to record his income on his tax returns, but that's besides the point.)
Still, time is money, so Nick's wondering if he's going about things as efficiently as possible. After all, Zootopia's a big place, so studying its map more carefully may suggest some improvements to his daily route.
Zootopia has key locations, with roads running amongst them. The road runs between distinct locations and , and can be travelled along in either direction in minutes. No pair of locations are directly connected by multiple roads.
For Nick's purposes, he's classified each location with a type . He has no interest in type- locations, but the other location types correspond to the steps of his Pawpsicle operation. Specifically, type- locations are ice cream parlors, type- locations are fields in Tundratown, type- locations are lemming banks, and type- locations are construction sites in Little Rodentia.
Nick starts each day in location , and needs to travel along a sequence of roads to visit a sequence of locations such that, at some point, he visits a type- location, then visits a type- location sometime later, then visits a type- location sometime after that, and finally visits a type- location sometime after that. He's considered to initially visit location , and on his way, he may travel through any locations as many times as he'd like.
In order to optimize the efficiency of his Pawpsicle operation, Nick would like to determine the minimum amount of time in which he can complete a route which visits the useful types of locations in the required order. Unfortunately, it may also turn out that no such route exists, in which case Nick may need to consider making a more honest living…
In test cases worth of the points, .
Input Specification
The first line of input consists of two space-separated integers and
.
lines follow, the of which consists of a single integer
(for ).
lines follow, the of which consists of three space-separated
integers , , and (for ).
Output Specification
Output one line consisting of a single integer - the minimum number of
minutes required for Nick to complete his Pawpsicle operation, or -1
if
it can't be done.
Sample Input
9 9
2 0 0 1 2 3 4 4 3
1 4 9
4 2 3
2 1 4
5 4 1
5 6 4
7 2 9
3 1 2
3 7 3
3 9 4
Sample Output
27
Sample Explanation
One optimal route which Nick can take is as follows (with the locations at which he'll carry out the steps of his Pawpsicle operator indicated with an asterisk ):
This route will take minutes to complete.
Comments