Under construction

Still being built. Things may change

Bottleneck Travelling Salesman Problem (Small)

Interview

Time limit1sMemory limit1024 MB

Summary
Given a small directed weighted graph, find a Hamiltonian cycle minimizing the largest edge cost on the cycle, and output that cost plus one optimal tour.
Level

Medium6 of 10

Topics
Graph, Brute force, Backtracking, Greedy
Solved
No attempts yet

Problem

The Traveling Salesman Problem (TSP) is one of the most important problems in computer science. Consider one of its variants, the Bottleneck Traveling Salesman Problem (BTSP).

You are given a directed graph with NN vertices and MM weighted edges. Starting at some vertex, you want to find a tour that visits each of the other N−1N-1 vertices exactly once and returns to the starting vertex. There can be several such tours, and you want to minimize the maximum edge cost along the tour.

Given a graph, find a tour that visits every vertex while minimizing the maximum edge cost.

Input

The first line contains the number of vertices NN and the number of edges MM. (2≤N≤92 \le N \le 9, 0≤M≤N(N−1)0 \le M \le N(N-1))

The next MM lines each contain three integers uu, vv, cc describing an edge. This means there is an edge from vertex uu to vertex vv with cost cc. No two edges in the same direction connect the same pair of vertices. (1≤u,v≤N1 \le u, v \le N, u≠vu \ne v, 1≤c≤5 000 0001 \le c \le 5 \, 000 \, 000)

Output

If a tour visiting every vertex exists, print the minimum possible maximum edge cost on the first line. If no such tour exists, print -1.

If a tour visiting every vertex exists, print on the next line the NN vertex numbers in the order they are visited, chosen so that the maximum edge cost is minimized. If several such tours exist, you may print any of them.

Examples2

  1. Example 1

    Input
    3 6
    1 2 4
    2 3 4
    3 1 4
    1 3 2
    3 2 3
    2 1 5
    
    Expected output
    4
    1 2 3
    
  2. Example 2

    Input
    2 1
    1 2 3
    
    Expected output
    -1