A Walk Through the Forest
InterviewTime limit1sMemory limit128 MB
Count the number of routes from intersection 1 to 2 in an undirected weighted graph where each step strictly decreases the shortest distance to intersection 2.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
Jimmy has been under a lot of stress at work lately, especially since an accident made working harder for him. To unwind after a long day, he likes to walk home. Even better, his office sits on one side of a forest and his house on the other, so his commute is a pleasant stroll among the birds and chipmunks.
The forest is beautiful, and Jimmy wants to take a different route every day. He also wants to reach home before dark, so he only ever walks along a path that makes progress toward his house.
Concretely, walking a path from intersection to intersection counts as progress if the shortest route from to his home is strictly shorter than the shortest route from to his home. Count how many different routes through the forest Jimmy might take from his office to his house.
Input
The input contains several test cases, followed by a line containing a single .
Every intersection (a point where paths meet) is numbered starting from . Jimmy's office is intersection and his house is intersection .
The first line of each test case contains the number of intersections () and the number of paths . Each of the next lines contains two intersections and and an integer distance (), describing a path of length between the two distinct intersections and . Jimmy may walk any path in either direction, and there is at most one path between any pair of intersections.
Output
For each test case, output a single integer: the number of different routes through the forest. You may assume this number does not exceed .