Shortest path algorithm Dijkstra algorithm (dijela algorithm), dijkstra Stella

Source: Internet
Author: User

Shortest path algorithm Dijkstra algorithm (dijela algorithm), dijkstra Stella
Shortest path algorithm Dijkstra algorithm (dijela algorithm) algorithm process

Source Network

Algorithm IDEA

Personal Understanding
In fact, it is to find the next shortest path using the known Shortest Path (any segment on the shortest path is the shortest path)
If the current time is shorter than the previous time, the distance is new.

Code
# Include <iostream> # include <stack> using namespace std; # define M 100 # define N 100 typedef struct MGraph {int matrix [N] [M]; int n; // number of fixed points int e; // Number of edges} MGraph; // dist [I] indicates the shortest distance from v0 to I found. // path [I] indicates the void Dij (MGraph g, int * dist, int * path, int v0) {bool * visted = new bool [g. n]; // The shortest distance int I found during initialization; for (I = 0; I <g. n; I ++) {visted [I] = false; if (g. matrix [v0] [I]> 0 & I! = V0) {dist [I] = g. matrix [v0] [I]; // the shortest path [I] = v0 from v0 to I found currently;} else {dist [I] = INT_MAX; path [I] =-1; // no path to this point} visted [v0] = true; dist [v0] = 0; path [v0] = v0; for (I = 1; I <g. n; I ++) {// find the shortest distance int mindist = INT_MAX; int u in the current distance record; // record the node corresponding to the shortest distance (int j = 0; j <g. n; j ++) {if (dist [j] <mindist & visted [j] = false) {mindist = dist [j]; u = j ;}} visted [u] = true; // the Shortest Path destined for u is found. // use this path to calculate the distance from another unaccessed point to for (int k = 0; k <g. n; k ++) {If (visted [k] = false & g. matrix [u] [k]> 0 & g. matrix [u] [k] + dist [u] <dist [k]) {dist [k] = g. matrix [u] [k] + dist [u]; path [k] = u ;}}// display the v0-> v Shortest path void showpath (int * path, int v0, int v) {stack <int> s; while (v! = V0) {s. push (v); v = path [v];} s. push (v); while (! S. empty () {cout <s. top () <"; s. pop () ;}} int main () {int n, e; while (cin >>n> e) {int * dist = new int [n]; int * path = new int [n]; int I, j, s, t, w, v0; MGraph g; g. n = n; g. e = e; for (I = 0; I <M; I ++) {for (j = 0; j <N; j ++) {g. matrix [I] [j] = 0 ;}}for (I = 0; I <e; I ++) {cin >>>> t> w; g. matrix [s] [t] = w;} cin> v0; Dij (g, dist, path, v0); for (I = 0; I <n; I ++) {if (I! = V0) {showpath (path, v0, I); cout <dist [I] <endl ;}} return 0 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.