The Floyd-warshall algorithm is a powerful weapon to solve the shortest path of any two points. It also applies to cases where there are negative edges.
DP idea, assuming that only the first k points are used, the shortest distance from I to J is d[k][i][j]
Then, the k+1 points can be used in two different situations.
The shortest ①i to J is used for the first k+1 (d[k+1][i][j] = d[k][i][j])
The shortest circuit ②i to J is not used for the k+1 point (d[k+1][i][j] = d[k][i][k]+d[k][k][j]);
So,d[k+1][i][j] = min (d[k][i][j],d[k][i][k]+d[k][k][j])
Using a scrolling array, you can write the form of a two-dimensional array
D[i][j] = min (D[i][j],d[i][k]+d[k][j])
Floyd algorithm can be solved in O (v^3) time, to determine whether there is a negative circle in the graph, only need to check if there is d[i][i] is a negative vertex I can be.
Code
d[I][J] represents the I->j value, which is not present when set to INF but d[I][I] set to 0for (int k = 0; k<V;k++) { for(int I=0;I<V;I++) { for(int J=0;J<V;J++) {D[I][J] =min(D[I][J],D[I][k]+D[k][J]); } }}
Because it is very simple to implement, if the complexity is within the tolerable range, the single source shortest circuit can be solved using the floyd-warshall algorithm.
In a forward graph, if you need to determine if there is a path for every two points, you can also use the Floyd algorithm for transitive closures .
Only need to change to d[i][j] = D[i][j] | | (D[i][k]&&d[k][j]) (Pretreatment also varies)
Arbitrary two-point shortest path floyd-warshall algorithm transitive closure