Book Shortest Path algorithm

Source: Internet
Author: User
Tags cmath

Use HDU2544 to sort out the shortest-path algorithm recently learned

1.Dijkstra algorithm

Principle: Set S Indicates the point where the shortest path has been found, d[] represents the distance from the current point to the source point

Initially, the collection has only the source points, when each point u enters the set S, with D[U]+W[U][V] to update the distance

Repeat this step to select the smallest entry set of d[] in all points outside s

Until all points are in the S collection

Limitation: The edge of the graph must be positive

Complexity: O (v*v), heap optimization ((e+v) LOGV)

(1) using adjacency matrix to implement

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A  -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=10005; - intW[MAXN][MAXN],D[MAXN],USED[MAXN]; - intn,m; +  - voidDijkstraints) { +memset (Used,0,sizeof(used)); A      for(intI=1; i<=n;i++) d[i]=INF; atd[s]=0; -      -      for(intk=1; k<=n;k++){ -         intp,m=INF; -          for(intI=1; i<=n;i++)if(!used[i]&&d[i]<m) m=d[p=i]; -used[p]=1; in          for(intI=1; i<=n;i++) D[i]=min (d[i],d[p]+w[p][i]); -     } to } +  - intMain () { the     inta,b,c; *      while(SCANF ("%d%d", &n,&m)!=eof&&n&&m) { $          for(intI=1; i<=n;i++){Panax Notoginseng              for(intj=1; j<=n;j++){ -                 if(I==J) w[i][j]=0; the                 Elsew[i][j]=INF; +             }             A         } the          +          for(intI=1; i<=m;i++){ -scanf" %d%d%d",&a,&b,&c); $w[a][b]=w[b][a]=C; $         } -Dijkstra1); -printf"%d\n", D[n]); the     } -     return 0;Wuyi}
View Code

(2) Using adjacency table to realize

Note Remember that each time you call Dijkstra (), first[] is set to 1

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A  -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=10005; - intD[MAXN],USED[MAXN],FIRSTT[MAXN],NEXTT[MAXN]; - intn,m,ecnt; +  - structedge{ +     intV,cost; A } E[MAXN]; at  - voidDijkstraints) { -memset (Used,0,sizeof(used)); -      for(intI=1; i<=n;i++) d[i]=INF; -d[s]=0; -      in      for(intk=1; k<=n;k++){ -         intp,m=INF; to          for(intI=1; i<=n;i++)if(!used[i]&&d[i]<m) m=d[p=i]; +used[p]=1; -          for(inti=firstt[p];i!=-1; i=Nextt[i]) { the             intv=e[i].v; *             if(d[v]>d[p]+e[i].cost) $d[v]=d[p]+E[i].cost;Panax Notoginseng         } -     } the } +  A voidAddedges (intUintVintW) { thenextt[++ecnt]=Firstt[u]; +e[ecnt].v=v; -e[ecnt].cost=W; $firstt[u]=ecnt;  $ } -  - intMain () { the     inta,b,c; -      while(SCANF ("%d%d", &n,&m)!=eof&&n&&m) {WuyiEcnt=0; thememset (firstt,-1,sizeof(FIRSTT)); -          Wu          for(intI=1; i<=m;i++){ -scanf" %d%d%d",&a,&b,&c); About addedges (a,b,c); $ addedges (b,a,c); -         } -Dijkstra1); -printf"%d\n", D[n]); A     } +     return 0; the}
View Code

Book Shortest Path algorithm

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.