Shortest Path --- Dijkstra Dijie Tesla algorithm --- Yan Weimin, Data Structure

Source: Internet
Author: User

// Exam1.cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream >#include <stack> using namespace STD; # define maxvex 20 # define int_max 10000 typedef struct arcnode {int adj; void * Info;} arcnode; typedef arcnode adjmat [maxvex] [maxvex]; typedef struct {int vexnum; int arcnum; adjmat am;} graph; void initgraph (graph & G) {cout <"Please enter the number of vertex:" <Endl; CIN> G. vexnum; cout <"Please enter the arc of the graph:" <Endl; cout <"Head tail weight" <Endl; For (INT I = 0; I <G. vexnum; I ++) {for (Int J = 0; j <G. vexnum; j ++) {if (I = J) {G. am [I] [J]. adj = 0;} else {G. am [I] [J]. adj = int_max ;}}int vex1, vex2, W; while (CIN >>> vex1 >> vex2 >> W) {G. am [vex1] [vex2]. adj = W ;}} void shortestpath_dij (graph G, bool ** path) {int * D; D = (int *) malloc (G. vexnum * sizeof (INT); bool * final; Final = (bool *) malloc (G. vexnum * sizeof (bool); memset (final, 0, G. vexnum * sizeof (bool); For (INT I = 0; I <G. vexnum; I ++) {d [I] = int_max; If (G. am [0] [I]. adj <int_max) {d [I] = G. am [0] [I]. adj;} path [I] [I] = true; // terminal pointpath [I] [0] = true; // starting point} final [0] = true; for (INT I = 1; I <G. vexnum; I ++) {int min = int_max; int min_indx =-1; for (Int J = 0; j <G. vexnum; j ++) {If (final [J] = false) {If (d [J] <min) {min = d [J]; min_indx = J ;}} final [min_indx] = true; If (min_indx! =-1) {for (Int J = 1; j <G. vexnum; j ++) {If (final [J] = false) {If (d [J]> d [min_indx] + G. am [min_indx] [J]. adj) {d [J] = d [min_indx] + G. am [min_indx] [J]. adj; For (int K = 0; k <G. vexnum; k ++) {path [J] [k] = path [min_indx] [k];} path [J] [J] = true ;}}}}} cout <"the shortest path from V0 to" <Endl; For (Int J = 0; j <G. vexnum; j ++) {cout <"v" <j <":" <D [J] <Endl; For (INT I = 0; I <G. vexnum; I ++) {cout <path [J] [I] <"" ;}cout <Endl ;}} int main (void) {graph G; initgraph (g); bool ** path; Path = (bool **) malloc (G. vexnum * sizeof (bool *); For (INT I = 0; I <G. vexnum; I ++) {path [I] = (bool *) malloc (G. vexnum * sizeof (bool); memset (path [I], false, G. vexnum * sizeof (bool);} shortestpath_dij (G, PATH); System ("pause"); 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.