Shortest Pilgrimage
Time limit5sMemory limit1024 MB
Find the shortest simple path from site 1 to site N that visits exactly eight distinct sites on a graph with weighted undirected roads.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Graph, Bit manipulation
- Solved
- No attempts yet
Problem
A pilgrim keeps making a pilgrimage that seems like it will never end. There are holy sites on the earth, numbered from 1 to . There are roads, and road lets you travel between sites and in minutes.
The pilgrim plans a new pilgrimage that starts at site 1 and ends at site . The pilgrimage visits exactly eight different sites, including sites 1 and . No site may be visited more than once.
Considering only the travel time on the roads, find the shortest time the pilgrimage takes.
Input
The first line contains the number of sites and the number of roads .
Each of the next lines contains road as , , (, , ). The same pair of sites is never connected by more than one road.
Output
Print the shortest time of the pilgrimage in minutes on the first line. If no pilgrimage satisfies the conditions, print -1.