Tour de France
Time limit2sMemory limit256 MB
Find the shortest directed tour that visits each of up to 36 cities exactly once when every city has at most two outgoing and two incoming roads.
- Level
Hard8 of 10
- Topics
- Backtracking, Graph
- Solved
- No attempts yet
Problem
The organisers of the Tour de France want the shortest possible route through the French cities they have selected. The route has to be a tour. It starts in one city, visits every other city exactly once, returns to the starting city, and each stage begins in the city where the previous stage ended.
Finding a shortest tour through an arbitrary road network is the travelling salesman problem, and the number of planned stages is far too large for that. So the organisers added one restriction. Every city offers at most two other cities as a destination, and every city accepts an incoming route from at most two cities. In graph terms the road network is a directed graph in which every vertex has out-degree at most and in-degree at most .
Roads are one-way. A road from city to city does not imply a road in the other direction, and when both exist their lengths can differ.
Given such a graph, compute the total length of the shortest tour that visits every city exactly once and returns to its starting city.
Input
The first line contains one integer , the number of test cases (). Each test case has the following format.
- One line with two space-separated integers and : the number of cities () and the number of roads ().
- lines, each with three space-separated integers , and (, , ), meaning there is a one-way road of length from city to city .
For any ordered pair at most one road from to is given, but the reverse road from to may also appear with a different length. Every city is the start of at most two roads and the end of at most two roads. Every test case admits at least one tour.
Output
For each test case, print one line with the length of the shortest tour through all cities.