NAJKRACI - Najkraci

no tags 

A road network in a country consists of N cities and M one-way roads. The cities are numbered 1 through N. For each road we know the origin and destination cities, as well as its length.

We say that the road F is a continuation of road E if the destination city of road E is the same as the origin city of road F. A path from city A to city B is a sequence of road such that origin of the first road is city A, each other road is a continuation of the one before it, and the destination of the last road is city B. The length of the path is the sum of lengths of all roads in it.

A path from A to B is a shortest path if there is no other path from A to B that is shorter in length.

Your task is to, for each road, output how many different shortest paths containing that road, modulo 1 000 000 007.


The first line contains two integers N and M (1 ≤ N ≤ 1500, 1 ≤ M ≤ 5000), the number of cities and roads.

Each of the following M lines contains three positive integers O, D and L. These represent a one-way road from city O to city D of length L. The numbers O and D will be different and L will be at most 10000.


Output M integers, each on its own line – for each road, the number of different shortest paths containing it, modulo 1 000 000 007. The order of these numbers should match the order of roads in the input.


4 4
1 2 5
2 3 5
3 4 5
1 4 8


5 8
1 2 20
1 3 2
2 3 2
4 2 3
4 2 3
3 4 5
4 3 5
5 4 20


Note: The test data for this problem consist of the official test cases from the contest, as well some cases of my own.

hide comments
xxbloodysantaxx: 2016-07-09 11:02:38

My Dirty way to get an AC. :D
Good problem.

Medo: 2016-06-11 02:43:42

My solution gives the same output as all all official test cases, but gets a wrong answer here....Are you sure your added test cases are correct ?

Last edit: 2016-06-11 02:43:57
Deepak: 2015-06-25 21:22:58

amazing problem..

Raghavendran Ramachandran: 2012-11-29 19:00:10

The sample test cases provided are really good. They provide clarity regarding many questions. For one, the given graph may be a multi-graph (which isn't immediately apparent from the problem description).

Hussain Kara Fallah: 2012-08-05 01:11:38

PLEASE I have a problem
i tried my program on the official testcases
in the testcases if i have two paths between two nodes and the first one is shorter that means the answer for the second one (the long path) >=0 not =0
and logically it is 0

Added by:Neal Wu
Time limit:0.177s-1.707s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO
Resource:Croatian Open 08/09 - Contest 3