The Dijkstra algorithm can be used as a precondition: there is no negative circle.
Negative circle: Negative ring, also known as negative ring, that is, a whole by the negative right side of the ring, so there is no shortest way, because each in the Ring transit path total length will be small.
Algorithm Description:
1. Find the shortest distance of the identified vertex, from which to update the shortest distance of adjacent vertices.
2. No longer need to care about the "minimum distance determined vertex" in 1.
C + + code:
#include <bits\stdc++.h>using namespacestd;#defineINF 2147483647#defineMax_v 1000#defineMax_e 2000//single Source Shortest path problem (Dijkstra algorithm)intCOST[MAX_V][MAX_V];//Cost[u][v] denotes the weight of E = (u,v)intD[MAX_V];//the shortest distance from the vertex s departureBOOLUSED[MAX_V];//Mark used pointsintV//number of verticesvoidDijkstraints) {Fill (d, D+V, INF); Fill (used, used+V, INF); D[s]=0; while(true){ intv =-1; //find a point that has not been used in the nearest distance for(intU =0; u < V; u++){ if(!used[u] && (v = =-1|| D[u] < d[v])) v =u; } //if all the points have been used, then break if(v = =-1) Break; //mark the current point has been usedUSED[V] =true; //update the distance of the points connected to the smallest point of the found distance for(intU =0; u < V; u++) {D[u]= Min (D[u], D[v] +Cost[v][u]); } }}intmain () {}
We will find that the adjacency matrix is particularly time-consuming and space-intensive if the edges are relatively small.
Time complexity O (v^2)
So if there is less side, there is a kind of adjacency matrix, to optimize it,
Time Complexity O (E*log (V))
C + + code:
#include <bits\stdc++.h>using namespacestd;#defineINF 2147483647#defineMax_v 1000#defineMax_e 2000//single Source Shortest path problem (Dijkstra algorithm)structedge{intTo,cost;}; typedef pair<int,int> P;//First is the shortest distance, second is the number of verticesintV//number of verticesVector <edge> G[max_v];//sideintD[MAX_V];//D[i] Indicates the shortest distance from the source point IvoidDijkstraints) { //by specifying the greater<p> parameter, the priority queue is implemented in heaps, and the heap is sorted from first to largest. Priority_queue<p, Vector<p>, greater<p> >que; Fill (d, D+V, INF); D[s]=0; //add source points into the minimum heapQue.push (P (0, s)); while(!Que.empty ()) { //The point at which the top of the heap is removed, which is the minimum distanceP p =que.top (); Que.pop (); intv =P.second; //If this point is updated after joining the queue, no more updates are required if(D[v] < P.first)Continue; //Traverse all points adjacent to the current point for(inti =0; i < g[v].size (); i++) {Edge e=G[v][i]; //If this point is able to update other points, then the updated point will be added to the queue. if(D[e.to] > D[v] +e.cost) {D[e.to]= D[v] +E.cost; Que.push (P (d[e.to], e.to)); } } }}intmain () {}
"Algorithm" Dijkstra algorithm (single source shortest path problem) adjacency Matrix and adjacency table implementation