Bug
Time limit1sMemory limit128 MB
Find the shortest route from city 1 to city n whose total length is odd, or report 0 if none exists.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
Your electronic calendar has a bug, the kind of defect programmers know well. Because of this bug, even integers cannot be entered into the calendar.
You are planning a business trip from Bytetown to Bitcity. Naturally, you want to travel along the shortest possible route. After you return, you must record the length of that route in the calendar, so the length has to be an odd integer.
Because the road network of Byteland will probably be rebuilt many times and the bug will not be fixed for a long while, you decide to write a program that can solve this kind of problem whenever it comes up.
Write a program that:
- reads a description of the map of Byteland from standard input,
- computes the length of the shortest odd-length route from Bytetown to Bitcity, or determines that no such route exists,
- writes the result to standard output.
Input
The first line contains two integers and separated by a single space (, ), the number of cities and the number of roads in Byteland. The cities are numbered from to ; Bytetown is city and Bitcity is city .
Each of the following lines describes one road with three space-separated integers , , (, , ), meaning there is a bidirectional road of length between city and city .
Output
Print a single integer: the length of the shortest odd-length route from Bytetown to Bitcity. The route may visit cities and roads multiple times. You may change the direction of travel (including turning back) only at a city. If no such route exists, print .
Hint
