#P6413. [COCI 2008/2009 #3] NAJKRACI

[COCI 2008/2009 #3] NAJKRACI

Description

There is a directed graph with nn vertices and mm edges.

For each edge, compute (modulo 109+710^9+7) how many times it is used by shortest paths between any pair of vertices.

If there are multiple shortest paths between vertices AA and BB, then each shortest path is counted once. You can refer to the output of the 3rd and 4th edges in Sample 4 to understand this sentence.

Input Format

The first line contains two integers nn and mm.

The next mm lines each contain three integers xx, yy, dd, meaning there is a directed edge from xx to yy with weight dd.

Output Format

Output mm lines.

Each line outputs one integer. The value on the ii-th line represents (modulo 109+710^9+7) the number of times the edge read on line i+1i+1 is used by shortest paths between any pair of vertices.

4 3
1 2 5
2 3 5
3 4 5 

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

2
3
2
1 
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 

0
4
6
6
6
7
2
6 
4 4
1 2 1
2 3 1
1 3 2
3 4 1

3
4
2
4

Hint

Constraints and Notes

  • For 30%30\% of the testdata, n15n \le 15, m30m \le 30.
  • For 60%60\% of the testdata, n300n \le 300, m103m \le 10^3.
  • For 100%100\% of the testdata, 1n1.5×1031 \le n \le 1.5\times 10^3, 1m5×1031 \le m \le 5\times 10^3, aba \neq b, 1a,bn1 \le a,b \le n, 1d1041 \le d \le 10^4.

Explanation

This problem is translated from Croatian Open Competition in Informatics 2008/2009, Contest #3, T6 NAJKRACI.

Translated by ChatGPT 5