C++ query shortest path example

  • 2020-04-02 02:22:56
  • OfStack


//shortest_path.c
#include<stdio.h>
#include<stdlib.h>//With the file
#include<string.h>//Available gets (), puts ()
#include"shortest_path.h"
#define MAX 32767
#define MENU " Welcome to the navigation system! n========== The menu ===========n0 , load the north field map n1 Create a map n2 , query the shortest path n3 , exit n========== The 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 open exception, read failed ");return -1;}
 fread(&map.spotnum,sizeof(int),1,fp);
 for(i=0;i<map.spotnum;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<map.spotnum;i++)
  for(j=0;j<map.spotnum;j++)
   fread(&map.pathmatrix[i][j],sizeof(int),1,fp);
 fclose(fp);
 return 0;
}
void loadmap()
{
 if(load1()==0)
  printf("spot Read the success n");
 else
  printf("spot Read the failure n");
 if(load2()==0)
  printf("path Read the success n");
 else
  printf("path Read the failure n"); 
}
void drawmap()//Direct input
{
 int i;
 int a,b;
 char s1[10],s2[10];
 printf(" How many spots are there? (<=20)");//map.spotmun
 fflush(stdin);
 scanf("%d",&map.spotnum);
 printf(" How many direct links are there between the scenic spots? ");//map.pathnum
 fflush(stdin);//Clear the keyboard buffer in "stdio.h"
 scanf("%d",&map.pathnum);
 for(a=0;a<map.spotnum;a++)//initialize map
  for(b=0;b<map.spotnum;b++)
  {
   if(a==b)map.pathmatrix[a][b]=0;
   else map.pathmatrix[a][b]=MAX;
  }
 for(i=0;i<map.spotnum;i++){
  printf(" Please enter the first %d The name of each attraction ( <=10letters ) ",i+1);
  fflush(stdin);
  gets(map.spot[i].name);
  printf(" Please enter the first %d An introduction to the four attractions ( <=20letters ) ",i+1);
  fflush(stdin);
  gets(map.spot[i].intro);
 }//Enter map.spot[].name; The map. The spot []. Intro
 for(i=0;i<map.pathnum;i++){
  do{
   printf(" Please enter the first %d The starting point of a path ",i+1);
   fflush(stdin);
   gets(s1);
   for(a=0;a<map.spotnum;a++)
    if(!strcmp(map.spot[a].name,s1))break;//Find the spot number
   if(a==map.spotnum)printf(" This scenic spot does not exist. Please enter again. n");
  }while(a==map.spotnum);
  do{
   printf(" Please enter the first %d The end of a path ",i+1);
   fflush(stdin);
   gets(s2);
   for(b=0;b<map.spotnum;b++)
    if(!strcmp(map.spot[b].name,s2))break;
   if(b==map.spotnum)printf(" This scenic spot does not exist. Please enter again. n");
  }while(b==map.spotnum);
  printf(" Please enter the first %d The length of the 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, input starting point, 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 the starting point ");//Find the starting spot number
  fflush(stdin);
  gets(s1);
  for(s=0;s<map.spotnum;s++)
   if(!strcmp(map.spot[s].name,s1))break;
  if(s==map.spotnum)printf(" This scenic spot does not exist. Please enter again. n");
 }while(s==map.spotnum); 
 do{
  printf(" Please enter endpoint ");//Find the destination's spot number
  fflush(stdin);
  gets(s2);
  for(t=0;t<map.spotnum;t++)
   if(!strcmp(map.spot[t].name,s2))break;
  if(t==map.spotnum)printf(" This scenic spot does not exist. Please enter again. n");
 }while(t==map.spotnum);
 for(v=0;v<map.spotnum;v++)//initialize spath
 {
  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<map.spotnum;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<map.spotnum;w++)
   if((!spath[w].in)&&(spath[w].length<min)){
    min=spath[w].length;
    v=w;
   }
  spath[v].in=1;
 }
 printf(" The shortest path length is zero %d, The 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 options (Numbers) n");
  fflush(stdin);
  scanf("%d",&menu);
  switch (menu)
  {
  case 0: loadmap();printf("n Map loading completed n");break;
  case 1: drawmap();printf("n The new complete n");break;
  case 2: shortestpath();printf("n Query completed n");break;
  }
 }
 printf(" Thanks for using! ");
}
2. [ file ] shortest_path.h ~ 430B      download (2)     
#ifndef _SHORTEST_PATH_H_
#define _SHORTEST_PATH_H_
struct stspot{//The apex of the attraction
 char name[11];//No more than 10
 char intro[20];//No more than 20
};
struct stmap{// The whole Undirected network
 stspot spot[20];//Dot, scenic spot vector
 int pathmatrix[20][20];//Edge, the adjacency matrix of paths
 int spotnum;
 int pathnum;
};
struct stspath//Find the shortest path when the array of attractions
{
 stspath * pior;
 int in;// can be boolen
 int length;
};
#endif



Related articles: