Shortest path algorithm learning Summary

Source: Internet
Author: User

Dijkstra shortest path algorithm:

The advantage of Dijkstra algorithm is that it can find the shortest distance from one point to all other points;

Input:

5 7
1 2 10
1 3 20
1 5 30
2 5 10
2 3 5
4 5 20
4 3 30

Output:

0 10 15 40 20 // This is the shortest distance from 1 to all vertices in this tree.

1 # include <iostream> 2 # include <cstring> 3 using namespace STD; 4 const int n = 1100; 5 const int INF = 1000000; 6 int map [N] [N]; 7 // int P [N]; 8 int vis [N]; 9 int low [N]; 10 int n, m; // represents the number of points and the number of routes 11 void Dijkstra (INT s) 12 {13 for (INT I = 1; I <= N; I ++) 14 {15 low [I] = map [s] [I]; 16} 17 low [s] = 0; 18 vis [s] =-1; 19 int V; 20 For (INT I = 1; I <n; I ++) 21 {22 int min = inf; 23 for (Int J = 1; j <= N; j ++) 24 {25 if (vis [J]! =-1 & low [J] <min) 26 {27 min = low [J]; 28 v = J; 29} 30} 31 vis [v] =-1; 32 For (Int J = 1; j <= N; j ++) 33 {34 if (vis [J]! =-1 & low [J]> low [v] + map [J] [v]) 35 low [J] = low [v] + map [J] [v]; // continuously update the shortest distance between two points 36} 37} 38} 39 int main () 40 {41 while (CIN> N> m) 42 {43 // memset (MAP, 0, sizeof (MAP); 44 for (INT I = 1; I <= N; I ++) 45 for (Int J = 1; j <= N; j ++) 46 map [I] [J] = inf; 47 memset (VIS, 0, sizeof (VIS); 48 int A, B, C; 49 for (int I (1); I <= m; I ++) 50 {51 scanf ("% d", & A, & B, & C ); 52 map [a] [B] = map [B] [a] = C; 53} 54 Dijkstra (1); 55 for (INT I = 1; I <= N; I ++) 56 {57 cout <low [I] <""; 58} 59 cout <Endl; 60} 61 Return 0; 62}

Floyd algorithm:

The Floyd algorithm is not suitable for processing large amounts of data because it is relatively free of space and time;

1 # include <iostream> 2 using namespace STD; 3 const int n = 300; // this field should not be too large, otherwise 4 const int INF = 11111111 will be reported; 5 Int main () 6 {7 int dis [N] [N]; 8 int n, m; // The number of points and the number of routes respectively 9 While (CIN> N> m) 10 {11 for (INT I = 1; I <= N; I ++) 12 for (Int J = 1; j <= N; j ++) 13 dis [I] [J] = inf; 14 for (INT I = 1; I <= N; I ++) 15 dis [I] [I] = 0; 16 int A, B, C; 17 for (INT I = 1; I <= m; I ++) 18 {19 scanf ("% d", & A, & B, & C ); 20 if (DIS [a] [B]> C) 21 dis [a] [B] = di S [B] [a] = C; 22} 23 for (int K = 1; k <= N; k ++) 24 for (INT I = 1; I <= N; I ++) 25 {26 for (Int J = I + 1; j <= N; j ++) 27 {28 If (DIS [I] [k]! = Inf & dis [k] [J]! = Inf & dis [I] [J]> dis [I] [k] + dis [k] [J]) 29 dis [J] [I] = dis [I] [J] = dis [I] [k] + dis [k] [J]; 30} 31} 32 int W, U; 33 scanf ("% d", & W, & U ); 34 if (DIS [u] [W] = inf) printf ("no solution \ n"); 35 else printf ("% d \ n ", dis [u] [W]); 36} 37 return 0; 38}

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.