The Bellman-Ford algorithm can be used to solve the situation where the required shortest path graph contains a negative edge. The basic idea of the algorithm: if there is a shortest path between two nodes, each node in this path goes through at most once (because if more than once, there is a ring in the path, if it is a positive number ring, the path weight will increase. If it is a negative ring, the shortest path does not exist. If it is a zero ring, the result will not be affected ). Therefore, we only need to iterate n-1 times to obtain the shortest path from the starting point to other points that can pass at most n-1 edges. [Cpp] # include <iostream> using namespace std; const int MaxSize = 10; int arr [MaxSize] [MaxSize]; www.2cto. comint dist [MaxSize]; // Save the int path [MaxSize] array from the start point to the shortest path of each node; // The array element saves the first node in the shortest path, int numNode = 0; void createArr () {cin> numNode; for (int I = 0; I <numNode; ++ I) for (int j = 0; j <numNode; ++ j) cin> arr [I] [j];} // Bellman-Ford Algorithm for calculating the shortest path of any weight // find the shortest path void BellmanFord (const int v) of all other fixed points from vertex v {// dist array and path Array initialization for (int I = 0; I <numNode; ++ I) {dist [I] = arr [v] [I]; if (I! = V) path [I] = v; else path [I] =-1;} // up to n-1 iterations for (int len = 2; len <numNode; ++ len) for (int u = 0; u <numNode; ++ u) if (u! = V) {// each time, the end point is u. Check whether the total weight of the vertex that reaches u is smaller than that of dist [u, // rewrite dist [u] for (int I = 0; I <numNode; ++ I) if (dist [u]> dist [I] + arr [I] [u]) {dist [u] = dist [I] + arr [I] [u]; path [u] = I ;}// output the shortest path from the Start Node to each node for (int I = 0; I <numNode; ++ I) cout <dist [I] <"; cout <endl; // outputs the nodes that pass through the Shortest Path of the last node (similar practices can be used for other nodes) int end = numNode-1; while (path [end]! =-1) {cout <path [end] <""; end = path [end] ;}cout <endl ;}int main () {createArr (); bellmanFord (0 );}