Icon:
Template:
1 /*2 Dijkstra calculates the single-source shortest path and records the path3 4 m points, n edges, the weights on each side are non-negative, the shortest path of the starting St to the Endpoint et is obtained5 6 Input:7 N M St et8 6 1 69 1 2 6Ten 1 3 2 One 1 4 1 A 2 3 6 - 2 5 3 - 3 4 2 the 3 5 2 - 3 6 4 - 4 6 5 - 5 6 3 + - Output: + 6 A 1-->4-->6 at */ - - - -#include <iostream> -#include <stdio.h> in#include <cstring> - using namespacestd; to + #defineINF 0XFFFFFFF - #defineMAXN 1010 the * intN,m;/*N: Points, M: Number of sides*/ $ intSt,et;/*ST: Starting point, et: end point*/Panax Notoginseng intWEIGHT[MAXN][MAXN];/*Preserve the edge weight value*/ - intDIS[MAXN];/*to save the shortest path between source points and any point*/ the intFATHER[MAXN];/*Save the Father node of I point*/ + intVIS[MAXN];/*record which vertices have been asked for the shortest path*/ A the + voidinput () - { $scanf"%d%d%d%d",&n,&m,&st,&ET); $ inti,j; - for(i=1; i<=n;i++) - { the for(j=1; j<=n;j++) -weight[i][j]=INF;Wuyiweight[i][i]=0; the } - intStart,end,value; Wu for(i=0; i<m;i++) - { Aboutscanf"%d%d%d",&start,&end,&value); $ if(Weight[start][end]>value)//Go heavy -Weight[start][end]=weight[end][start]=value;//graph without Direction - } - } A + voidDijkstra () the { - inti,j; $memset (Vis,0,sizeof(Vis)); thememset (father,0,sizeof(father)); the the //initializing the DIS array the for(i=1; i<=n;i++) -dis[i]=INF; indis[st]=0; the the //enumeration of N points About for(i=1; i<=n;i++) the { the intpos=-1; the + //find the shortest point that is not joined to the collection - for(j=1; j<=n;j++) the if(!vis[j]&& (pos==-1|| dis[j]<Dis[pos]))Bayipos=J; the the //Mark this point has gone through -vis[pos]=1; - the //Update Dis the for(j=1; j<=n;j++) the if(!vis[j]&& (dis[j]>dis[pos]+Weight[pos][j])) the { -dis[j]=dis[pos]+Weight[pos][j]; thefather[j]=Pos; the } the }94 } the the voidoutput () the {98printf"%d\n", Dis[et]); About intQUE[MAXN]; - intCnt=0;101 inttmp=et;102 while(father[tmp]!=0)103 {104que[cnt++]=tmp; thetmp=father[tmp];106 }107 inti;108printf"%d", ST);109 for(i=cnt-1; i>=0; i--) theprintf"-->%d", Que[i]);111printf"\ n"); the }113 the the intMain () the {117 input ();118 Dijkstra ();119 output (); - return 0;121}
Variant 1:pat 1018: http://pat.zju.edu.cn/contests/pat-a-practise/1018
Dijkstra algorithm and deformation of "graph algorithm"