Given a directed graph where each camp has exactly two departing and two arriving tours with daily departure hours and durations, find the fastest route that uses every tour once and returns to camp 1.
Hard8GraphDynamic programmingGreedyImplementationNo attempts yetTime limit5sMemory limit512 MBYou are on top of Mount Everest, and you want to enjoy every hiking trail up there. Wandering around Mount Everest alone is a bad idea, because you can get lost in the dark. So you only take tours that leave at fixed times with a guide.
The mountain has C camps, numbered 1 through C, and 2C one way hiking tours, numbered 1 through 2C. Each tour starts at one camp, finishes at a different camp, and passes through no other camp in between. Mount Everest is sparsely populated and business is slow. Exactly 2 tours depart from each camp, and exactly 2 tours arrive at each camp.
Every tour runs daily. Tours 1 and 2 start at camp 1, tours 3 and 4 start at camp 2, and in general tour 2i−1 and tour 2i start at camp i. Tour i ends at camp Ei, leaves at hour Li, and takes exactly Di hours.
It is now hour 0, and the hours of a day are numbered 0 through 23. You are at camp 1, and you want to take every tour exactly once and end up back at camp 1. You cannot travel between camps except on a tour. While you are in a camp you may wait for any number of hours, including zero, but you can start a tour only at the instant it departs.
The schedules make the goal reachable. You want to finish as fast as possible. Plan the route optimally and report how many hours it takes to finish all of the tours.
The first line holds the number of test cases T. Each test case begins with one line holding the integer C, the number of camps. Then 2C lines follow. The i-th of those lines, counting from 1, describes one tour that starts at camp ⌊(i+1)/2⌋ and holds three integers Ei, Li, Di. This format guarantees that exactly two tours start at each camp.
Limits
For each test case, print one line of the form Case #x: y, where x is the test case number starting from 1 and y is the minimum number of hours needed to reach the goal.
In the first test case, the optimal plan is the following.
That reaches the goal in 1 day and 8 hours, which is 32 hours. Every other plan takes longer.
In the second test case, all of the tours leave at the same hour and take the same time. After finishing any tour you can start another one right away. If the tours are numbered 1 through 8 in the order they appear in the input, one optimal plan is 1, 5, 4, 7, 6, 2, 3, 8.