Bus Driver Seungjae

Time limit3sMemory limit128 MB

Summary
Given a graph with hotels, a start, and a sightseeing spot, find the shortest closed route that picks up and drops off each hotel while keeping half of the drop-offs within the first half of pick-ups.
Level

Hard8 of 10

Topics
Graph, Shortest path, Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

Seungjae drives buses for the famous tour company ALPS. His job is to leave the ALPS headquarters by bus, pick up one tourist from each of the hh hotels, take them all to the sightseeing spot, then return every tourist to their own hotel and drive back to the ALPS headquarters. The only sightseeing spot ALPS uses is a single fixed location, so the bus always visits just that one spot.

Because ALPS prides itself on service, it tries to drop tourists off roughly in the order they were picked up. Concretely, ALPS follows this rule: when the tourists are picked up from the hh hotels in some order, every tourist whose pick-up position is within the first h/2h/2 must also have a drop-off position within the first h/2h/2.

For example, suppose five tourists are picked up in the order 1 2 3 4 5. Then dropping them off as 2 1 3 4 5, or as 1 2 5 3 4, is allowed, but dropping them off as 1 3 2 4 5 is not: the tourist picked up 2nd is dropped off 3rd, and 33 is greater than 5/25/2.

Seungjae wants to save fuel, so he wants the total distance the bus travels in a day to be as small as possible. Given the travel times between the ALPS headquarters, the hotels, and the sightseeing spot, compute the minimum possible total distance of a route that obeys the rule.

Because the rule fixes only the order of pick-ups and drop-offs, obeying it may keep the bus from taking an otherwise shorter route, and the bus may sometimes have to drive past a hotel without stopping. You only need to find the shortest route that still obeys the rule.

Input

Each test case begins with a line containing the number of vertices nn (3≤n≤203 \le n \le 20) and the number of edges mm (2≤m2 \le m). Here nn counts the hotels, the sightseeing spot, and the starting point together.

Vertices are numbered 00 to n−1n-1. Vertex 00 is the bus's starting point, vertex n−1n-1 is the sightseeing spot, and vertices 11 through n−2n-2 are the hotels.

The next mm lines each contain three integers uu, vv, and tt (0≤u,v≤n−10 \le u, v \le n-1, 1≤t≤36001 \le t \le 3600), meaning it takes time tt to travel from uu to vv. Roads are two-way, so travelling from vv to uu also takes time tt.

You may assume that at least one path exists between any pair of vertices. The input contains several test cases; process them until the end of the input.

Output

For each test case, print Case t: d, where t is the test case number (starting from 1) and d is the minimum total travel distance of a route that obeys the rule.

Examples1

  1. Example 1

    Input
    5 4
    0 1 10
    1 2 20
    2 3 30
    3 4 40
    4 6
    0 1 1
    0 2 1
    0 3 1
    1 2 1
    1 3 1
    2 3 1
    
    Expected output
    Case 1: 300
    Case 2: 6