Solving the shortest-circuit problem between all two points is called the shortest path between any two points.
Can be solved with dynamic programming,
D[K][I][J] Indicates the shortest path length using only the top k vertices and vertex I to the vertex J.
There are two kinds of situations to discuss:
1. After vertex k, d[k][i][j] = d[k-1][i][j]. That is equal to the shortest path when only the first k-1 vertices are used
2. Do not go through the vertex k, d[k][i][j] = D[k-1][i][k] + d[k-1][k][j]. That is, the shortest path from K is equal to the +j path of I away from K.
Can get recursive d[k][i][j] = min (D[k-1][i][j], d[k-1][i][k] + d[k-1][s][k]);
There are also two-dimensional representations:
D[i][j] = min (D[i][j], d[i][k] + d[k][j]);
Lite algorithm Templates:
#include <bits\stdc++.h>using namespacestd;#defineINF 2147483647#defineMax_v 1000#defineMax_e 2000intD[MAX_V][MAX_V];//D[u][v] Indicates the weight of the Edge e= (u,v) (not present when set to INF, but d[i][i] = 0)intV//fixed-point numbervoidWarshall_floyd () { for(intK =0; k < V; k++){ for(inti =0; i < V; i++){ for(intj =0; J < V; J + +) {D[i][j]= Min (D[i][j], d[i][k] +D[k][j]); } } }} intmain () {}
Negative Circle judgment:
Judge the negative circle only by judging whether there is d[i][i] is negative.
"Algorithm" Floyd-warshall algorithm (shortest-circuit problem between any two points) (judging negative circle)