This page is still under construction.

Parts of this page are still being built. What you see may change.

A Walk Through the Forest

Interview

Time limit1sMemory limit128 MB

Summary
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 AA to intersection BB counts as progress if the shortest route from BB to his home is strictly shorter than the shortest route from AA 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 00.

Every intersection (a point where paths meet) is numbered starting from 11. Jimmy's office is intersection 11 and his house is intersection 22.

The first line of each test case contains the number of intersections NN (1<N≤10001 < N \le 1000) and the number of paths MM. Each of the next MM lines contains two intersections aa and bb and an integer distance dd (1≤d≤10000001 \le d \le 1000000), describing a path of length dd between the two distinct intersections aa and bb. 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 21474836472147483647.

Examples1

  1. Example 1

    Input
    5 6
    1 3 2
    1 4 2
    3 4 3
    1 5 12
    4 2 34
    5 2 24
    7 8
    1 3 1
    1 4 1
    3 7 1
    7 4 1
    7 5 1
    6 7 1
    5 2 1
    6 2 1
    0
    
    Expected output
    2
    4