Example of the shortest path query in c ++

Source: Internet
Author: User
Tags fread

This article describes how to query the Shortest Path in c ++. For more information, see

The Code is as follows:

// Shortest_path.c

# Include

# Include // use file

# Include // available gets (), puts ()

# Include "shortest_path.h"

# Deprecision MAX 32767

# Define MENU "welcome to the navigation system! \ N ============ menu =================\ n0, load map \ n1, create map \ n2, query shortest path \ n3, exit \ n ========== menu ===========\ n"

Struct stmap map; // undirected network

Const char * filepath1 = "D: \ spots. dat ";

Const char * filepath2 = "D: \ paths. dat ";

Int load1 ()

{

FILE * fp;

Int I;

Fp = fopen (filepath1, "r ");

If (fp = NULL) {printf ("spots file opening exception, read failed"); return-1 ;}

Fread (& map. spotnum, sizeof (int), 1, fp );

For (I = 0; I

{

Fread (map. spot [I]. name, sizeof (char), 10, fp );

Fread (map. spot [I]. intro, sizeof (char), 20, fp );

}

Fclose (fp );

Return 0;

}

Int load2 ()

{

FILE * fp;

Int I, j;

Fp = fopen (filepath2, "r ");

If (fp = NULL) {printf ("abnormal opening of the paths file, failed to read"); return-1 ;}

Fread (& map. pathmatrix, sizeof (int), 1, fp );

For (I = 0; I

For (j = 0; j

Fread (& map. pathmatrix [I] [j], sizeof (int), 1, fp );

Fclose (fp );

Return 0;

}

Void loadmap ()

{

If (load1 () = 0)

Printf ("spot read succeeded \ n ");

Else

Printf ("failed to read spot \ n ");

If (load2 () = 0)

Printf ("path read successfully \ n ");

Else

Printf ("path failed to read \ n ");

}

Void drawmap () // input directly

{

Int I;

Int a, B;

Char s1 [10], s2 [10];

Printf ("How many scenic spots are there? (<= 20) "); // map. spotmun

Fflush (stdin );

Scanf ("% d", & map. spotnum );

Printf ("How many scenic spots are directly connected to each other? "); // Map. pathnum

Fflush (stdin); // clear the Keyboard Buffer, in "stdio. h"

Scanf ("% d", & map. pathnum );

For (a = 0;

For (B = 0; B

{

If (a = B) map. pathmatrix [a] [B] = 0;

Else map. pathmatrix [a] [B] = MAX;

}

For (I = 0; I

Printf ("enter the name of the % d scenic spot (<= 10 letters)", I + 1 );

Fflush (stdin );

Gets (map. spot [I]. name );

Printf ("enter an introduction to the % d scenic spots (<= 20 letters)", I + 1 );

Fflush (stdin );

Gets (map. spot [I]. intro );

} // Enter the Scenic spot name and description map. spot []. name; map. spot []. intro

For (I = 0; I

Do {

Printf ("Enter the starting point of PATH % d", I + 1 );

Fflush (stdin );

Gets (s1 );

For (a = 0;

If (! Strcmp (map. spot [a]. name, s1) break; // find the scenic spot number

If (a = map. spotnum) printf ("this attraction does not exist. Please enter it again. \ N ");

} While (a = map. spotnum );

Do {

Printf ("Enter the end point of PATH % d", I + 1 );

Fflush (stdin );

Gets (s2 );

For (B = 0; B

If (! Strcmp (map. spot [B]. name, s2) break;

If (B = map. spotnum) printf ("this attraction does not exist. Please enter it again. \ N ");

} While (B = map. spotnum );

Printf ("Enter the length of PATH % d", I + 1 );

Fflush (stdin );

Scanf ("% d", & map. pathmatrix [a] [B]);

Map. pathmatrix [B] [a] = map. pathmatrix [a] [B]; // input path length

}

}

Void shortestpath () // shortest path, which is used to input the output path and distance from the start point.

{

Struct stspath spath [20];

Int s, t, v, w, min;

Char s1 [10], s2 [10];

Int pathorder [20];

Struct stspath * p; // pathorder

Do {

Printf ("Enter the Starting Point"); // you can specify the starting point.

Fflush (stdin );

Gets (s1 );

For (s = 0; s

If (! Strcmp (map. spot [s]. name, s1) break;

If (s = map. spotnum) printf ("this attraction does not exist. Please enter it again. \ N ");

} While (s = map. spotnum );

Do {

Printf ("Enter the destination"); // find the destination scenic spot number

Fflush (stdin );

Gets (s2 );

For (t = 0; t

If (! Strcmp (map. spot [t]. name, s2) break;

If (t = map. spotnum) printf ("this attraction does not exist. Please enter it again. \ N ");

} While (t = map. spotnum );

For (v = 0; v

{

Spath [v]. length = MAX;

Spath [v]. in = 0;

}

Spath [s]. in = 1;

Spath [s]. length = 0;

Spath [s]. pior = NULL;

V = s;

While (v! = T ){

For (w = 0; w

If ((! Spath [w]. in) & (spath [w]. length> spath [v]. length + map. pathmatrix [v] [w]) {

Spath [w]. length = spath [v]. length + map. pathmatrix [v] [w];

Spath [w]. pior = & spath [v];

}

Min = MAX;

For (w = 0; w

If ((! Spath [w]. in) & (spath [w]. length

Min = spath [w]. length;

V = w;

}

Spath [v]. in = 1;

}

Printf ("Shortest path length: % d, shortest path: \ n", spath [t]. length); // print path

For (v = 0, p = & spath [t]; p-> pior! = NULL; p = p-> pior)

{

Pathorder [v] = p-spath;

V ++;

}

Pathorder [v] = s;

For (; v> = 0; v --)

Printf ("% 10 s", map. spot [pathorder [v]. name );

}

Void main ()

{

Int menu = 1;

Printf (MENU );

While (menu! = 3)

{

Printf ("\ n Please enter your option (number) \ n ");

Fflush (stdin );

Scanf ("% d", & menu );

Switch (menu)

{

Case 0: loadmap (); printf ("\ n \ n"); break;

Case 1: drawmap (); printf ("\ n created \ n"); break;

Case 2: shortestpath (); printf ("\ n query completed \ n"); break;

}

}

Printf ("Thank you! ");

}

2. [file] shortest_path.h ~ 430B download (2)

# Ifndef _ SHORTEST_PATH_H _

# Define _ SHORTEST_PATH_H _

Struct stspot {// vertex of a scenic spot

Char name [11]; // scenic spot name no more than 10

Char intro [20]; // no more than 20

};

Struct stmap {// The entire undirected network

Stspot spot [20]; // point, scenic spot Vector

Int pathmatrix [20] [20]; // edge, the adjacent matrix of the path

Int spotnum;

Int pathnum;

};

Struct stspath // list of scenic spots when finding the Shortest Path

{

Stspath * pior;

Int in; // can be boolen

Int length;

};

# Endif

Note: For more exciting articles, please pay attention to the Programming Tutorial section of the Helper house.

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.