Yeongil Land
InterviewTime limit1sMemory limit1024 MB
Find the longest directed route from the gate that rides each of N rides (N up to 9) exactly once and returns to the gate.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Bit manipulation, Graph
- Solved
- No attempts yet
Problem
Yeongil Land is a theme park made up of one main gate and rides, each with an identification number. The gate is numbered 0, and the rides are numbered 1 through . Directed edges connect the gate and rides, or one ride and another. Two places may be joined by several edges, and an edge may start and end at the same place.
Jeongmin visits Yeongil Land, starts at the gate, and wants to know the longest time of a route that rides every ride exactly once and returns to the gate. The rides are so fascinating that Jeongmin cannot pass one without riding it, so each ride can be reached at most once. Also, the gate cannot be visited until every ride has been ridden, and ride time is ignored.
Find the longest time for the curious Jeongmin.
Input
The first line contains the number of rides . ()
The second line contains the number of edges . ()
Each of the next lines contains , , and , separated by spaces. This is an edge from the gate or ride to the gate or ride , and it takes to travel. The sum of all travel times is less than . (; )
Output
Print the longest time of a route that starts at the gate, rides every ride exactly once, and returns to the gate. If no such route exists, print -1.