Shortest path algorithm II-Dijkstra Algorithm

Source: Internet
Author: User

Dijkstra Algorithm

The Dijkstra algorithm is mainly characterized by layer-by-layer expansion centered on the starting point until the end point.

Note that this algorithm requires that no negative weight edge exists in the graph.

First, we define a two-dimensional array edge [maxn] [maxn] to store Graph Information.

 

After the edge array of the graph is initialized

 

We also need to use a one-dimensional array DIS to store the initial distance from vertex 1 to the other vertices, as shown below.

 

The DIS array contains the estimated value of the shortest path.

After the Dijkstra algorithm is used to relax, DIS stores the exact value (Shortest Path) from the initial point to each point.

 

The Dijkstra algorithm is implemented as follows (take hdu1548 as an example ):

 

 1 #include<stdio.h> 2 #include<limits.h> 3 #include<iostream> 4 #include<string.h> 5 #define MAXN 200 6 using namespace std; 7 int edge[MAXN+10][MAXN+10]; 8 int dis[MAXN+10]; 9 bool vis[MAXN+10];10 int T,S,D,N,k;11 void dijkstra(int begin)12 {13     memset(vis,0,sizeof(vis));14     for (int i=1; i<=T; i++)15         dis[i]=INT_MAX;16     dis[begin]=0;17     for (int t=1; t<=T; t++)18     {19         vis[begin]=1;20         for (int i=1; i<=T; i++)21             if (!vis[i]&&edge[begin][i]!=INT_MAX&&dis[begin]+edge[begin][i]<dis[i])22                 dis[i]=dis[begin]+edge[begin][i];23         int min=INT_MAX;24         for (int j=1; j<=T; j++)25             if (!vis[j]&&min>dis[j])26             {27                 min=dis[j];28                 begin=j;29             }30     }31 }32 int main()33 {34     int begin,end;35     while (cin>>T)36     {37         if (T==0) break;38         for (int i=1; i<=MAXN; i++)39             for (int j=1; j<=MAXN; j++)40                 edge[i][j]=INT_MAX;41         scanf("%d %d",&begin,&end);42         int t;43         for (int i=1; i<=T; i++)44         {45             scanf("%d",&t);46             if (i+t<=T) edge[i][i+t]=1;47             if (i-t>=1) edge[i][i-t]=1;48         }49         dijkstra(begin);50         if (dis[end]!=INT_MAX) printf("%d\n",dis[end]);51         else printf("-1\n");52     }53     return 0;54 }

 

Time Complexity: O (N ^ 2)

After using the adjacent table (see below), the result can reach O (mlogn)

PS: m may be n ^ 2 in the worst case !!

Some of the image text is taken from the blog of Aha lei.

 

Related Article

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.