最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c++查詢最短路徑示例

 更新時間:2014年05月04日 11:11:05   作者:  
這篇文章主要介紹了c++查詢最短路徑示例,需要的朋友可以參考下

復制代碼 代碼如下:

//shortest_path.c
#include<stdio.h>
#include<stdlib.h>//用file
#include<string.h>//可用gets(),puts()
#include"shortest_path.h"
#define MAX 32767
#define MENU "歡迎進入導航系統(tǒng)!\n==========菜單===========\n0、載入北外地圖\n1、建立地圖\n2、查詢最短路徑\n3、退出\n==========菜單===========\n"

struct stmap map;//無向網
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文件打開異常,讀取失敗");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文件打開異常,讀取失敗");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讀入成功\n");
 else
  printf("spot讀入失敗\n");

 if(load2()==0)
  printf("path讀入成功\n");
 else
  printf("path讀入失敗\n"); 
}

void drawmap()//直接輸入
{
 int i;
 int a,b;
 char s1[10],s2[10];
 printf("共有幾個景點?(<=20)");//map.spotmun
 fflush(stdin);
 scanf("%d",&map.spotnum);
 printf("共有幾條景點與景點之間直接相連的路徑?");//map.pathnum
 fflush(stdin);//清空鍵盤緩沖區(qū),在"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("請輸入第%d個景點的名稱(<=10letters)",i+1);
  fflush(stdin);
  gets(map.spot[i].name);
  printf("請輸入第%d個景點的介紹(<=20letters)",i+1);
  fflush(stdin);
  gets(map.spot[i].intro);
 }//輸入景點名字和簡介map.spot[].name;map.spot[].intro
 for(i=0;i<map.pathnum;i++){
  do{
   printf("請輸入第%d條路徑的起點",i+1);
   fflush(stdin);
   gets(s1);
   for(a=0;a<map.spotnum;a++)
    if(!strcmp(map.spot[a].name,s1))break;//查找景點編號
   if(a==map.spotnum)printf("不存在此景點,請重新輸入。\n");
  }while(a==map.spotnum);
  do{
   printf("請輸入第%d條路徑的終點",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("不存在此景點,請重新輸入。\n");
  }while(b==map.spotnum);
  printf("請輸入第%d條路徑的長度",i+1);
  fflush(stdin);
  scanf("%d",&map.pathmatrix[a][b]);
  map.pathmatrix[b][a]=map.pathmatrix[a][b];//輸入路徑長度
 }
}

void shortestpath()//最短路徑,輸入起點終點輸出路徑和路程
{
 struct stspath spath[20];
 int s,t,v,w,min;
 char s1[10],s2[10];
 int pathorder[20];
 struct stspath *p;//pathorder
 do{
  printf("請輸入起點");//查找起點的景點編號
  fflush(stdin);
  gets(s1);
  for(s=0;s<map.spotnum;s++)
   if(!strcmp(map.spot[s].name,s1))break;
  if(s==map.spotnum)printf("不存在此景點,請重新輸入。\n");
 }while(s==map.spotnum); 
 do{
  printf("請輸入終點");//查找終點的景點編號
  fflush(stdin);
  gets(s2);
  for(t=0;t<map.spotnum;t++)
   if(!strcmp(map.spot[t].name,s2))break;
  if(t==map.spotnum)printf("不存在此景點,請重新輸入。\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("最短路徑長度為%d,最短路徑為:\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請輸入您的選項(數(shù)字)\n");
  fflush(stdin);
  scanf("%d",&menu);
  switch (menu)
  {
  case 0: loadmap();printf("\n北外地圖載入完成\n");break;
  case 1: drawmap();printf("\n新建完成\n");break;
  case 2: shortestpath();printf("\n查詢完成完成\n");break;
  }
 }
 printf("謝謝使用!");
}
2. [文件] shortest_path.h ~ 430B     下載(2)    
#ifndef _SHORTEST_PATH_H_
#define _SHORTEST_PATH_H_

struct stspot{//景點的頂點
 char name[11];//景點名稱no more than 10
 char intro[20];//景點介紹no more than 20
};

struct stmap{//整個無向網
 stspot spot[20];//點,景點向量
 int pathmatrix[20][20];//邊,路徑的鄰接矩陣
 int spotnum;
 int pathnum;
};

struct stspath//求最短路徑時的景點數(shù)組
{
 stspath * pior;
 int in;// can be boolen
 int length;
};

#endif



相關文章

  • C/C++?單元自動化測試解決方案總結

    C/C++?單元自動化測試解決方案總結

    這篇文章主要介紹了C/C++?單元自動化測試解決方案總結,通過利用GCC插件來實現(xiàn)提升C/C++開發(fā)者的單元效率工具解決方案,希望對大家在提升單元測試效率上有所啟發(fā)
    2022-06-06
  • C語言中getchar和putchar的使用方法詳解

    C語言中getchar和putchar的使用方法詳解

    我們知道scanf函數(shù)可以從鍵盤輸入信息,而printf則可以輸出信息,同樣地,getchar和putchar也有同樣的功能,下面我來給大家介紹putchar和getchar的使用方法,需要的朋友可以參考下
    2023-08-08
  • C++實現(xiàn)設計模式之裝飾者模式詳解

    C++實現(xiàn)設計模式之裝飾者模式詳解

    這篇文章主要介紹了C++設計模式之裝飾模式,裝飾模式能夠實現(xiàn)動態(tài)的為對象添加功能,是從一個對象外部來給對象添加功能,需要的朋友可以參考下
    2021-09-09
  • C語言中可變參數(shù)的使用方法示例

    C語言中可變參數(shù)的使用方法示例

    這篇文章主要給大家介紹了關于C語言中可變參數(shù)的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • C++的array和&array有什么區(qū)別

    C++的array和&array有什么區(qū)別

    本文主要介紹了C++的array和&array有什么區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • C++中平衡二叉搜索樹的模擬實現(xiàn)

    C++中平衡二叉搜索樹的模擬實現(xiàn)

    二叉搜索樹雖可以縮短查找的效率,但如果數(shù)據有序或接近有序二叉搜索樹將退化為單支樹,查找元素相當于在順序表中搜索元素,效率低下,所以本文給大家介紹了C++平衡二叉的搜索樹模擬實現(xiàn)方法,需要的朋友可以參考下
    2023-09-09
  • Qt定時器和隨機數(shù)詳解

    Qt定時器和隨機數(shù)詳解

    在前一篇中我們介紹了鍵盤和鼠標事件,其實還有一個非常常用的事件,就是定時器事件,如果要對程序實現(xiàn)時間上的控制,那么就要使用到定時器。而隨機數(shù)也是很常用的一個功能,在我們要想產生一個隨機的結果時就要使用到隨機數(shù)。本文我們就來簡單介紹一下定時器和隨機數(shù)。
    2015-06-06
  • C++基于遞歸和非遞歸算法求二叉樹鏡像的方法

    C++基于遞歸和非遞歸算法求二叉樹鏡像的方法

    這篇文章主要介紹了C++基于遞歸和非遞歸算法求二叉樹鏡像的方法,針對二叉樹遍歷結合實例形式分析了遞歸與非遞歸算法的實現(xiàn)與使用技巧,需要的朋友可以參考下
    2017-05-05
  • Qt基礎開發(fā)之Qt文件操作類QFile讀寫文件的詳細方法與實例及QDataStream的使用方法

    Qt基礎開發(fā)之Qt文件操作類QFile讀寫文件的詳細方法與實例及QDataStream的使用方法

    這篇文章主要介紹了Qt基礎開發(fā)之Qt文件操作類QFile讀寫文件的詳細方法與實例,需要的朋友可以參考下
    2020-03-03
  • C語言實現(xiàn)餐飲結賬管理系統(tǒng)

    C語言實現(xiàn)餐飲結賬管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)餐飲結賬管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11

最新評論

文成县| 新乐市| 宁明县| 石楼县| 惠水县| 迁西县| 东宁县| 滕州市| 东方市| 嘉禾县| 延川县| 同德县| 揭阳市| 米泉市| 桓仁| 依兰县| 博湖县| 托里县| 乌鲁木齐县| 扶风县| 广河县| 油尖旺区| 苗栗市| 十堰市| 广汉市| 灵璧县| 大连市| 东乡| 沂南县| 长沙市| 明水县| 理塘县| 莒南县| 金堂县| 板桥市| 呼玛县| 商河县| 大港区| 河池市| 德惠市| 赣榆县|