Example of the shortest path for C + + queries

Source: Internet
Author: User
Tags fread min printf stdin strcmp

This article mainly introduces the C + + query Shortest Path example, the need for friends can refer to the following

The code is as follows:

Shortest_path.c

#include

#include//with file

#include//Available gets (), puts ()

#include "shortest_path.h"

#define MAX 32767

#define MENU "Welcome to the navigation system!n========== menus ===========n0, loading the map n1, building maps N2, query the shortest path N3, exit n========== menu ===========n"

struct Stmap map;//without 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 open 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 ("Paths file open exception, read failed"); 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 reads success n");

Else

printf ("spot reads failed n");

if (Load2 () ==0)

printf ("Path reads success n");

Else

printf ("path reads failed n");

}

void Drawmap ()/Direct input

{

int i;

int a,b;

Char s1[10],s2[10];

printf ("How many attractions are there?" ( <=20) ");//map.spotmun

Fflush (stdin);

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

printf ("A direct link between several attractions and attractions"); /map.pathnum

Fflush (stdin);//empty keyboard buffer, in "stdio.h"

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

for (a=0;a

for (b=0;b

{

if (a==b) map.pathmatrix[a][b]=0;

else Map.pathmatrix[a][b]=max;

}

for (i=0;i

printf ("Please enter the name of the first%d attractions (<=10letters)", i+1);

Fflush (stdin);

Gets (Map.spot[i].name);

printf ("Please enter the introduction of the first%d attractions (<=20letters)", i+1);

Fflush (stdin);

Gets (Map.spot[i].intro);

}//Enter the name and description of the attraction Map.spot[].name;map.spot[].intro

for (i=0;i

do{

printf ("Please enter the start of the%d path", i+1);

Fflush (stdin);

Gets (S1);

for (a=0;a

if (!strcmp (MAP.SPOT[A].NAME,S1)) break;//Find the spot number

if (a==map.spotnum) printf ("Does not exist, please re-enter it.") n ");

}while (A==map.spotnum);

do{

printf ("Please enter the end of page%d path", i+1);

Fflush (stdin);

Gets (S2);

for (b=0;b

if (!strcmp (MAP.SPOT[B].NAME,S2)) break;

if (b==map.spotnum) printf ("Does not exist, please re-enter it.") n ");

}while (B==map.spotnum);

printf ("Please enter the length of section%d path", 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, enter start point end output path and distance

{

struct Stspath spath[20];

int s,t,v,w,min;

Char s1[10],s2[10];

int pathorder[20];

struct Stspath *p;//pathorder

do{

printf ("Please enter a starting point");//Find the spot number for the starting point

Fflush (stdin);

Gets (S1);

for (s=0;s

if (!strcmp (MAP.SPOT[S].NAME,S1)) break;

if (s==map.spotnum) printf ("Does not exist, please re-enter it.") n ");

}while (S==map.spotnum);

do{

printf ("Please enter End");//Find the destination number for the end point

Fflush (stdin);

Gets (S2);

for (t=0;t

if (!strcmp (MAP.SPOT[T].NAME,S2)) break;

if (t==map.spotnum) printf ("Does not exist, please re-enter it.") 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 is%d, shortest path is: 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 ("%10s", 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 ();p rintf ("N map loading n");

Case 1:drawmap ();p rintf ("N New Complete n");

Case 2:shortestpath ();p rintf ("N query complete n");

}

}

printf ("Thank you for using!");

}

2. [File] shortest_path.h ~ 430B Download (2)

#ifndef _shortest_path_h_

#define _shortest_path_h_

Vertices of struct stspot{//sights

Char name[11];//attractions name no more than 10

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

};

struct stmap{//whole non-direction net

Stspot spot[20];//dot, attraction vector

int pathmatrix[20][20];//edge, path adjacency matrix

int spotnum;

int pathnum;

};

struct stspath//An array of sights when seeking the shortest path

{

Stspath * PIOR;

int in;//can be Boolen

int length;

};

#endif

Note : Please pay attention to the triple Programming Tutorials section for more wonderful articles .

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.