Under construction

Still being built. Things may change

Floyd-Warshall

Time limit5sMemory limit256 MB

Summary
Given a directed weighted graph, count the ordered vertex pairs whose distance is wrong when Floyd-Warshall runs with the loops reordered to y, z, x.
Level

Medium7 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Statement

Radewoosh has a directed weighted graph with nn vertices. He needs the distance between every pair of vertices, so he decided to use the Floyd-Warshall algorithm.

A correct implementation of Floyd-Warshall is as follows.

  1. MM is an n×nn \times n matrix. Its initial values are:

Mi,j={0,if i=jwi,j,if there is an edge from i to j with weight wi,j∞otherwiseM_{i,j} = \begin{cases} 0, & \text{if } i = j \\ w_{i,j}, & \text{if there is an edge from } i \text{ to } j \text{ with weight } w_{i,j} \\ \infty & \text{otherwise} \end{cases}

  1. For x=1,2,3,…,nx = 1, 2, 3, \dots, n do the following.
  2. For y=1,2,3,…,ny = 1, 2, 3, \dots, n do the following.
  3. For z=1,2,3,…,nz = 1, 2, 3, \dots, n do the following.
  4. My,z←min⁡(My,z,My,x+Mx,z)M_{y,z} \leftarrow \min(M_{y,z}, M_{y,x} + M_{x,z})

Radewoosh changed the order of the loops, and his algorithm is now incorrect.

His incorrect implementation is as follows.

  1. MM is the n×nn \times n matrix defined above.
  2. For y=1,2,3,…,ny = 1, 2, 3, \dots, n do the following.
  3. For z=1,2,3,…,nz = 1, 2, 3, \dots, n do the following.
  4. For x=1,2,3,…,nx = 1, 2, 3, \dots, n do the following.
  5. My,z←min⁡(My,z,My,x+Mx,z)M_{y,z} \leftarrow \min(M_{y,z}, M_{y,x} + M_{x,z})

How many ordered pairs of vertices have a distance that Radewoosh's algorithm computes incorrectly?

Input

The first line contains two integers nn and mm (2≤n≤2 0002 \le n \le 2\,000, 1≤m≤3 0001 \le m \le 3\,000), the number of vertices and the number of edges.

Each of the next mm lines contains three integers uiu_i, viv_i, wiw_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \neq v_i, 1≤wi≤100 0001 \le w_i \le 100\,000). This means the ii-th edge goes from uiu_i to viv_i with weight wiw_i. No ordered pair (ui,vi)(u_i, v_i) appears more than once.

Output

Print one integer: the number of ordered pairs of vertices whose distance is computed incorrectly by Radewoosh's algorithm.

Examples1

  1. Example 1

    Input
    4 5
    2 3 4
    3 4 3
    4 2 2
    1 3 1
    1 2 9
    
    Expected output
    1