This page is still under construction.

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

Road Trip

Interview

Time limit1sMemory limit128 MB

Summary
Given a weighted tree rooted at city 1, remove exactly one non-root vertex so the round trip from city 1 covering all remaining cities is shortest, and report that length.
Level

Medium5 of 10

Topics
Tree, DFS, Graph, Implementation
Solved
No attempts yet

Problem

A traveling band wants to play a show in every major city in their state and then return to the city they started from. After looking at the cost of renting a venue in each city, they realize their tight budget lets them skip playing in exactly one city.

The band has already chosen which roads to use, and the chosen roads contain no cycles while still connecting every city — that is, the chosen roads form a tree. Each road is two-way and may be driven any number of times.

The band always starts and finishes at city 1, so they can never skip city 1. When they skip a city, that city and the roads touching it are removed, and every remaining city must still be reachable over the chosen roads. Among all cities they are allowed to skip, they pick the one that makes their round trip as short as possible.

Output the length of that shortest possible round trip.

Input

The first line contains the number KK of data sets. The KK data sets follow, each in the form described below.

The first line of a data set contains two integers VV and EE, the number of cities and the number of roads, where 2≤V≤1002 \le V \le 100 and 1≤E≤1001 \le E \le 100.

Each of the next EE lines describes one two-way road with three integers aia_i, bib_i, did_i, meaning there is a road of length did_i between city aia_i and city bib_i (1≤ai,bi≤V1 \le a_i, b_i \le V). The roads contain no cycles, so they form a tree that connects all VV cities. The band always starts at city 1.

Output

For each data set, print a line of the form Data Set x:, where xx is the number of the data set (starting from 1). On the next line, print the minimum distance the band travels when it skips the best single city, visits every remaining city, and returns to city 1. Print one blank line after each data set.

Examples1

  1. Example 1

    Input
    1
    5 4
    1 2 3
    2 3 7
    3 4 3
    3 5 4
    
    Expected output
    Data Set 1:
    26