Problem 279
Count Shortest Paths
Hard · 30 pts · Graph, Shortest Path, Dynamic Programming
Problem
You are given an undirected graph with N nodes and M edges with positive weights. Count how many different paths from node 1 to node N have the minimum possible total weight, and print the count modulo 1000000007. If node N cannot be reached, print 0. Run Dijkstra's algorithm and keep a path count per node. When a shorter distance to a neighbour is found, its count becomes the count of the current node. When an equally short distance is found, add the current node's count to it. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).
Input
First line: N and M. Next M lines: an edge u v w.
Output
Print the number of shortest paths modulo 1000000007.
Constraints
1 <= N <= 200; 0 <= M <= 20000; 1 <= w <= 1000000
Example input
4 5 1 2 1 2 4 1 1 3 1 3 4 1 1 4 5
Example output
2
Explanation
The shortest distance from 1 to 4 is 2, reached by 1 → 2 → 4 and by 1 → 3 → 4. The direct edge 1-4 costs 5, which is longer. So there are 2 shortest paths.