This page is still under construction.

Parts of this page are still being built. What you see may change.

Ambulance Antics

Time limit2sMemory limit256 MB

Summary
Plan tours from the hospital that carry up to three patients each to bring every patient back in the least total driving time.
Level

Medium7 of 10

Topics
Dynamic programming, Shortest path, Combinatorics
Solved
No attempts yet

Problem

One ambulance has to carry patients who are scattered across the city to the hospital. The ambulance holds at most three patients at a time, and it has to drive back to the hospital to drop patients off. Loading and unloading take no time.

The city consists of intersections and streets. One intersection holds the hospital, and every other intersection holds one waiting patient. Every street is bidirectional, and each street has a driving time in minutes that is the same in both directions. The ambulance starts at the hospital.

The ambulance may pass through any intersection as many times as it likes. Compute the smallest number of minutes needed to bring every patient to the hospital. The ambulance stands at the hospital once the last patient is dropped off.

Input

The first line contains the number of test cases TT.

The first line of each test case contains two integers NN and MM, the number of intersections holding a patient and the number of streets. Each of the next MM lines contains three integers aia_i, bib_i, cic_i, meaning that a bidirectional street connects intersections aia_i and bib_i and that driving along it takes cic_i minutes.

The city has N+1N + 1 intersections, numbered 0 to NN. The hospital is at intersection NN, and one patient waits at each of the intersections 0 through N−1N - 1.

  • 0<T≤1000 < T \le 100
  • 1≤N≤201 \le N \le 20
  • M>0M > 0
  • 0≤ai,bi≤N0 \le a_i, b_i \le N
  • 0<ci≤1000000 < c_i \le 100000
  • A route always exists between any two intersections.
  • At most one street connects a given pair of intersections.

Output

For each test case, print one line with the minimum number of minutes needed to deliver every patient to the hospital.

Examples2

  1. Example 1

    Input
    1
    2 2
    0 1 10
    1 2 10
    
    Expected output
    40
    
  2. Example 2

    Input
    3
    1 1
    0 1 100000
    2 3
    0 1 1
    1 2 1
    0 2 1
    5 5
    0 1 4
    1 2 4
    2 3 4
    3 4 4
    4 5 4
    
    Expected output
    200000
    3
    56