Cheapest Closed Route
Time limit1sMemory limit128 MB
Given an undirected weighted graph, find the minimum total weight of a non-empty closed walk that never repeats an edge, or report none exists.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Greedy, Brute force
- Solved
- No attempts yet
Problem
Byteasar is planning an excursion through Byteland. Some pairs of cities are joined by two-way bus connections. Byteasar wants a trip that starts and ends in the same city and never uses the same bus connection twice: once he has ridden the connection between two cities in either direction, he will not ride that connection again. The fare of such a closed route is the sum of the fares of the connections it uses.
Among all non-empty closed routes that use no bus connection more than once, find the smallest possible total fare, or report that no such route exists.
Input
The first line contains two integers and separated by a single space (, ): the number of cities and the number of two-way bus connections.
Each of the next lines contains three integers , and (, , ): a connection between cities and with fare . Each pair of cities is joined by at most one connection.
Output
Print a single line with the minimum possible total fare of a non-empty closed route that uses no bus connection more than once. If no such route exists, print BRAK instead.