Floyd-Warshall
Time limit5sMemory limit256 MB
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 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.
- is an matrix. Its initial values are:
- For do the following.
- For do the following.
- For do the following.
Radewoosh changed the order of the loops, and his algorithm is now incorrect.
His incorrect implementation is as follows.
- is the matrix defined above.
- For do the following.
- For do the following.
- For do the following.
How many ordered pairs of vertices have a distance that Radewoosh's algorithm computes incorrectly?
Input
The first line contains two integers and (, ), the number of vertices and the number of edges.
Each of the next lines contains three integers , , (, , ). This means the -th edge goes from to with weight . No ordered pair appears more than once.
Output
Print one integer: the number of ordered pairs of vertices whose distance is computed incorrectly by Radewoosh's algorithm.