// 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 ;}