c语言 dijkstra算法 交通图

来源:互联网 发布:广电怎么看网络电视 编辑:程序博客网 时间:2024/05/19 13:56

数据结构课程设计做的 要求是做自己学校的交通图

不知道程序的效率怎么样

请各位大神指点 小弟不胜感激



类型声明:

#ifndef STATEMENT_H_INCLUDED#define STATEMENT_H_INCLUDED#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAXNUMVETEX 25#define NotAVetex (-1)#define MINWEIGHT (-1)#define INFINITY 10000/*later ues math.h*/#define LeftChild(i) (2*(i))#define RightChild(i) (2*(i)+1)#define Parent(i) ((i)/2)#define GraphLEN sizeof(struct GraphAdjacencyList)#define EdgeLEN sizeof(struct EdgeNode)#define PriorityQueueLEN sizeof(struct Heap)#define TableLEN sizeof(struct TableEntry)#define VetexNodeLEN sizeof(struct VetexNode)struct VetexNode;struct EdgeNode;struct AdjacencyListNode;struct GraphAdjacencyList;typedef struct EdgeNode *Edge;typedef struct VetexNode AdjacencyList;typedef struct GraphAdjacencyList *Graph;typedef struct TableEntry Table;typedef struct TableEntry *PtrToTable;typedef struct Heap *PriorityQueue;struct EdgeNode{    int VetexId;    char Name[20];    double Dist;    double Time;    Edge Next;};struct VetexNode{    int NumEdge;    int IsClassRoom;    int Id;    char Name[20];    Edge Header;};struct GraphAdjacencyList{    int NumVetex,NumEdge;    AdjacencyList *AdList;};struct TableEntry{    int Known;    int VetexId;    int Path;    double Dist;    double Time;};struct Heap{    int Size;    int Capacity;    PtrToTable *Element;};#endif // STATEMENT_H_INCLUDED
导入地图的程序

#include "statement.h"int FindVetexIdBaseName(char str[],Graph G){    int i;    for(i=0;i<G->NumVetex&&strcmp(str,G->AdList[i].Name)!=0;i++){}    return (i);}int IsChinese(char str[]){    int i;   for(i=0;str[i]!='\0';i++)   {       if(str[i]>=0&&!(str[i]>='0'&&str[i]<='9'))        return 0;   }   return 1;}Graph CreatGraph(void){    int i,Cell1,Cell2;    double TmpDist,TmpTime;    Graph G;    char Tmpstr1[20],Tmpstr2[20];    Edge TmpEdge;    G=malloc(GraphLEN);    puts("input the Number of Vetex:");    scanf("%d",&G->NumVetex);    G->AdList=calloc(G->NumVetex,VetexNodeLEN);    puts("input the Number of Edge:");    scanf("%d",&G->NumEdge);    puts("Now Input The Vetex Information:");    puts("Every Place'Name Must Use Chinese Language Or Number!");    puts("");    for(i=0;i<G->NumVetex;i++)    {        G->AdList[i].NumEdge=0;        G->AdList[i].IsClassRoom=0;        G->AdList[i].Id=i;        do        {            printf("Input The No.%d Vetex's Name:",i+1);            scanf("%s",G->AdList[i].Name);        }while(!IsChinese(G->AdList[i].Name));        printf("Is This Vetex A ClassRoom?(1/0) :");        scanf("%d",&G->AdList[i].IsClassRoom);        TmpEdge=malloc(EdgeLEN);        TmpEdge->Next=NULL;        TmpEdge->Time=0;        TmpEdge->Dist=0;        strcpy(TmpEdge->Name,G->AdList[i].Name);        TmpEdge->VetexId=G->AdList[i].Id;        G->AdList[i].Header=TmpEdge;        TmpEdge=NULL;    }    puts("Now Input The Edge Information:");    puts("There Is Space Between Two Place's Name!");    puts("");    for(i=0;i<G->NumEdge;i++)    {        printf("No.%d Edge:Input Two Vetex's Name:",i+1);        scanf("%s %s",Tmpstr1,Tmpstr2);        Cell1=FindVetexIdBaseName(Tmpstr1,G);        if(strcmp(Tmpstr1,G->AdList[Cell1].Name)!=0)        {            puts("Not have this Vetex!,Input Again!");            i--;            continue;        }        Cell2=FindVetexIdBaseName(Tmpstr2,G);        if(strcmp(Tmpstr2,G->AdList[Cell2].Name)!=0)        {            puts("Not have this Vetex!,Input Again!");            i--;            continue;        }        G->AdList[Cell1].NumEdge++;G->AdList[Cell2].NumEdge++;        puts("Input the Distance Of Two Vetex:");        scanf("%lf",&TmpDist);        puts("Input the Time-Cost Of Two Vetex:");        scanf("%lf",&TmpTime);        TmpEdge=malloc(EdgeLEN);        TmpEdge->Dist=TmpDist;        TmpEdge->Time=TmpTime;        strcpy(TmpEdge->Name,G->AdList[Cell2].Name);        TmpEdge->VetexId=G->AdList[Cell2].Id;        TmpEdge->Next=G->AdList[Cell1].Header;        G->AdList[Cell1].Header=TmpEdge;/*Let Cell2 Vetex Linked Cell1 To Make A Edge*/        TmpEdge=malloc(EdgeLEN);        TmpEdge->Dist=TmpDist;        TmpEdge->Time=TmpTime;        strcpy(TmpEdge->Name,G->AdList[Cell1].Name);        TmpEdge->VetexId=G->AdList[Cell1].Id;        TmpEdge->Next=G->AdList[Cell2].Header;        G->AdList[Cell2].Header=TmpEdge;/*Let Cell1 Vetex Linked Cell1 To Make A Edge*/    }    return G;}void SaveGraph(Graph G){    int i;    FILE *fp;    Edge HeadEdge;    if((fp=fopen("Graph.dat","wb"))==NULL)    {        puts("can't open the file!");        exit(0);    }    else    {        fwrite(G,GraphLEN,1,fp);        fwrite(G->AdList,VetexNodeLEN*G->NumVetex,1,fp);        for(i=0;i<G->NumVetex;i++)        {            HeadEdge=G->AdList[i].Header;            while(HeadEdge!=NULL)            {                fwrite(HeadEdge,EdgeLEN,1,fp);                HeadEdge=HeadEdge->Next;            }        }    }    fclose(fp);    puts("\t\t\tSave Successfully!");}Graph ReadGraph(void){    int i,j;    FILE *fp;    Graph G;    Edge HeadEdge,TmpEdge;    G=malloc(GraphLEN);    if((fp=fopen("Graph.dat","rb"))==NULL)    {        puts("can't open the file!");        exit(0);    }    else    {        fread(G,GraphLEN,1,fp);        G->AdList=calloc(G->NumVetex,VetexNodeLEN);        fread(G->AdList,VetexNodeLEN*G->NumVetex,1,fp);        for(i=0;i<G->NumVetex;i++)        {            HeadEdge=malloc(EdgeLEN);            fread(HeadEdge,EdgeLEN,1,fp);            G->AdList[i].Header=HeadEdge;            for(j=1;j<G->AdList[i].NumEdge;j++)            {                TmpEdge=malloc(EdgeLEN);                fread(TmpEdge,EdgeLEN,1,fp);                HeadEdge->Next=TmpEdge;                HeadEdge=TmpEdge;            }            TmpEdge->Next=NULL;            HeadEdge=NULL;        }    }    fclose(fp);    puts("\t\t\tRead Successfully!");    return (G);}void PrintGraph(Graph G){    int i,j;    Edge Ptr;    for(i=0;i<G->NumVetex;i++)    {        printf("%d:%s %dEdges:\t\n",G->AdList[i].Id,G->AdList[i].Name,G->AdList[i].NumEdge);        Ptr=G->AdList[i].Header;        while(Ptr!=NULL)        {            printf("\t\t\t%.1lfMile And %.1lfMinute->%d:%s\n",Ptr->Dist,Ptr->Time,Ptr->VetexId,Ptr->Name);            Ptr=Ptr->Next;        }        printf("\n\n");    }}void Test(Graph G){    Edge TmpEdge;    int i;    for(i=0;i<G->NumVetex;i++)    {        TmpEdge=malloc(EdgeLEN);        TmpEdge->Next=NULL;        TmpEdge->Time=0;        TmpEdge->Dist=0;        strcpy(TmpEdge->Name,G->AdList[i].Name);        TmpEdge->VetexId=G->AdList[i].Id;        TmpEdge->Next=G->AdList[i].Header;        G->AdList[i].Header=TmpEdge;        G->AdList[i].NumEdge++;    }}int main(){    Graph G;    G=CreatGraph();    PrintGraph(G);    SaveGraph(G);    return 0;}

交通图导航程序

#include "statement.h"int IsChinese(char str[])/*judge place's name is chinese word or not*/{    int i;   for(i=0;str[i]!='\0';i++)   {       if(str[i]>=0&&!(str[i]>='0'&&str[i]<='9'))        return 0;   }   return 1;}void DestroyTable(Table T[]){    free(T);}//PriorityQueue's operationvoid DestroyPriorityQueue(PriorityQueue P){    free(P->Element);    free(P);    P=NULL;}int IsFull(PriorityQueue P){    return (P->Capacity==P->Size);}int IsEmpty(PriorityQueue P){    return (P->Size==0);}PriorityQueue CreatPriorityQueue(int Capacity){    int i;    PriorityQueue P;    Table T;    P=malloc(PriorityQueueLEN);    P->Element=calloc(Capacity+1,sizeof(struct TableEntry *));    P->Capacity=Capacity;    P->Size=0;    for(i=0;i<Capacity+1;i++)    {        P->Element[i]==NULL;    }    T.Dist=MINWEIGHT;    T.Time=MINWEIGHT;    T.VetexId=NotAVetex;    P->Element[0]=&T;    return P;}void PercolateDownBaseDist(PriorityQueue P){    int child;    int i;    PtrToTable Tmp;    Tmp=P->Element[1];    for(i=1,child=LeftChild(i);child<=P->Size;i=child,child=LeftChild(i))    {        if(child!=P->Size&&P->Element[child]->Dist>P->Element[child+1]->Dist)        {            child++;        }        if(Tmp->Dist>P->Element[child]->Dist)        {            P->Element[i]=P->Element[child];        }        else            break;    }    P->Element[i]=Tmp;}void PercolateDownBaseTime(PriorityQueue P){    int child;    int i;    PtrToTable Tmp;    Tmp=P->Element[1];    for(i=1,child=LeftChild(i);child<=P->Size;i=child,child=LeftChild(i))    {        if(child!=P->Size&&P->Element[child]->Time>P->Element[child+1]->Time)        {            child++;        }        if(Tmp->Time>P->Element[child]->Time)        {            P->Element[i]=P->Element[child];        }        else            break;    }    P->Element[i]=Tmp;}void PercolateUpBaseDist(PriorityQueue P){    int parent;    int i;    PtrToTable Tmp;    Tmp=P->Element[P->Size];    for(i=P->Size,parent=Parent(i);parent>0&&Tmp->Dist<P->Element[parent]->Dist;i=parent,parent=Parent(i))    {        P->Element[i]=P->Element[parent];    }    P->Element[i]=Tmp;}void PercolateUpBaseTime(PriorityQueue P){    int parent;    int i;    PtrToTable Tmp;    Tmp=P->Element[P->Size];    for(i=P->Size,parent=Parent(i);parent>=0&&Tmp->Time<P->Element[parent]->Time;i=parent,parent=Parent(i))    {        P->Element[i]=P->Element[parent];    }    P->Element[i]=Tmp;}void InsertBaseDist(PtrToTable T,PriorityQueue P){    if(IsFull(P))    {        puts("The Priority Is Full!");        exit(0);    }    else    {        P->Element[++P->Size]=T;        PercolateUpBaseDist(P);    }}void InsertBaseTime(PtrToTable T,PriorityQueue P){    if(IsFull(P))    {        puts("The Priority Is Full!");        exit(0);    }    else    {        P->Element[++P->Size]=T;        PercolateUpBaseTime(P);    }}PtrToTable DeletMinBaseDist(PriorityQueue P){    if(IsEmpty(P))    {        puts("The PriorityQueue Is Empty!");        exit(0);    }    PtrToTable Tmp;    Tmp=P->Element[1];    P->Element[1]=P->Element[P->Size--];    PercolateDownBaseDist(P);    return (Tmp);}PtrToTable DeletMinBaseTime(PriorityQueue P){    if(IsEmpty(P))    {        puts("The PriorityQueue Is Empty!");        exit(0);    }    PtrToTable Tmp;    Tmp=P->Element[1];    P->Element[1]=P->Element[P->Size--];    PercolateDownBaseTime(P);    return (Tmp);}/*PriorityQueue's operation*/int FindVetexIdBaseName(char str[],Graph G)/*Find Vetex Id Base on the Place's Name*/{    int i;    for(i=0;i<G->NumVetex&&strcmp(str,G->AdList[i].Name)!=0;i++){}    return (i);}void SaveGraph(Graph G)/*save Adjacency List*/{    int i;    FILE *fp;    if((fp=fopen("Graph.dat","wb"))==NULL)    {        puts("can't open the file!");        exit(0);    }    else    {        fwrite(G,GraphLEN,1,fp);        for(i=0;i<MAXNUMVETEX;i++)        {            fwrite(&G->AdList[i],sizeof(struct VetexNode),1,fp);        }    }    fclose(fp);    puts("Save Successfully!");}Graph ReadGraph(void)/*Read Adjacency List*/{    int i,j;    FILE *fp;    Graph G;    Edge HeadEdge,TmpEdge;    G=malloc(GraphLEN);    if((fp=fopen("Graph.dat","rb"))==NULL)    {        puts("can't open the file!");        exit(0);    }    else    {        fread(G,GraphLEN,1,fp);        G->AdList=calloc(G->NumVetex,VetexNodeLEN);        fread(G->AdList,VetexNodeLEN*G->NumVetex,1,fp);        for(i=0;i<G->NumVetex;i++)        {            HeadEdge=malloc(EdgeLEN);            fread(HeadEdge,EdgeLEN,1,fp);            G->AdList[i].Header=HeadEdge;            for(j=1;j<G->AdList[i].NumEdge;j++)            {                TmpEdge=malloc(EdgeLEN);                fread(TmpEdge,EdgeLEN,1,fp);                HeadEdge->Next=TmpEdge;                HeadEdge=TmpEdge;            }            HeadEdge=NULL;        }    }    fclose(fp);    puts("\t\t\t地图导入成功!");    return (G);}void InitializeDistTable(int VetexId,Graph G,Table T[]){    int i;    for(i=0;i<G->NumVetex;i++)    {        T[i].Known=0;        T[i].VetexId=i;        T[i].Dist=INFINITY;        T[i].Path=NotAVetex;    }    T[VetexId].Dist=0;}void InitializeTimeTable(int VetexId,Graph G,Table T[])/*VetexId is the start Vetex*/{    int i;    for(i=0;i<G->NumVetex;i++)    {        T[i].Known=0;        T[i].VetexId=i;        T[i].Time=INFINITY;        T[i].Path=NotAVetex;    }    T[VetexId].Time=0;}void PrintPath(int VetexId,Table T[],Graph G)/*Use Recursive Print Path*/{    if(T[VetexId].Path!=NotAVetex)    {        PrintPath(T[VetexId].Path,T,G);        printf("->");    }    printf("%s",G->AdList[VetexId].Name);}void Dijkstra(Graph G,Table DistTable[],Table TimeTable[]){    int i;    Edge TmpEdge;    int VeTexId1,VetexId2;    PtrToTable TmpPtrTable;    PriorityQueue P;    P=CreatPriorityQueue(G->NumVetex);    for(i=0;DistTable[i].Dist!=0;i++){}    InsertBaseDist(&DistTable[i],P);    while(!IsEmpty(P))    {        TmpPtrTable=DeletMinBaseDist(P);        if(!TmpPtrTable->Known)        {            TmpPtrTable->Known=1;            TmpEdge=G->AdList[TmpPtrTable->VetexId].Header;            while(TmpEdge!=NULL)            {                if(!DistTable[TmpEdge->VetexId].Known)                {                    if(TmpPtrTable->Dist+TmpEdge->Dist<DistTable[TmpEdge->VetexId].Dist)                    {                        DistTable[TmpEdge->VetexId].Dist=TmpPtrTable->Dist+TmpEdge->Dist;                        DistTable[TmpEdge->VetexId].Time=TmpPtrTable->Time+TmpEdge->Time;                        InsertBaseDist(&DistTable[TmpEdge->VetexId],P);                        DistTable[TmpEdge->VetexId].Path=TmpPtrTable->VetexId;                    }                }                TmpEdge=TmpEdge->Next;            }        }    }    DestroyPriorityQueue(P);    P=CreatPriorityQueue(G->NumVetex);    for(i=0;TimeTable[i].Time!=0;i++){}    InsertBaseTime(&TimeTable[i],P);    while(!IsEmpty(P))    {        TmpPtrTable=DeletMinBaseTime(P);        if(!TmpPtrTable->Known)        {            TmpPtrTable->Known=1;            TmpEdge=G->AdList[TmpPtrTable->VetexId].Header;            while(TmpEdge!=NULL)            {                if(!TimeTable[TmpEdge->VetexId].Known)                {                    if(TmpPtrTable->Time+TmpEdge->Time<TimeTable[TmpEdge->VetexId].Time)                    {                        TimeTable[TmpEdge->VetexId].Time=TmpPtrTable->Time+TmpEdge->Time;                        TimeTable[TmpEdge->VetexId].Dist=TmpPtrTable->Dist+TmpEdge->Dist;                        InsertBaseTime(&TimeTable[TmpEdge->VetexId],P);                        TimeTable[TmpEdge->VetexId].Path=TmpPtrTable->VetexId;                    }                }                TmpEdge=TmpEdge->Next;            }        }    }}void ShortestPath(Table *DistTable,Table *TimeTable,int StartVetexId,Graph G)/*Dijkstra algorithm's DriveFunction*/{    InitializeDistTable(StartVetexId,G,DistTable);    InitializeTimeTable(StartVetexId,G,TimeTable);    Dijkstra(G,DistTable,TimeTable);}void NearestClassRoom(Graph G,Table DistTable[],Table TimeTable[])/*最近的教室功能(学霸寻路)*/{    int i;    int MinDistRoom,MinTimeRoom;    PtrToTable TmpPtrTable;    PriorityQueue DistP;    PriorityQueue TimeP;    DistP=CreatPriorityQueue(G->NumVetex);    TimeP=CreatPriorityQueue(G->NumVetex);    for(i=0;i<G->NumVetex;i++)    {        if(G->AdList[i].IsClassRoom)        {            InsertBaseDist(&DistTable[i],DistP);            InsertBaseTime(&TimeTable[i],TimeP);        }    }    TmpPtrTable=DeletMinBaseDist(DistP);    MinDistRoom=TmpPtrTable->VetexId;    TmpPtrTable=DeletMinBaseTime(TimeP);    MinTimeRoom=TmpPtrTable->VetexId;    printf("距离最近的教室:%s\n",G->AdList[MinDistRoom].Name);    printf("行程:%.1lf \t花费时间:%.1lf\n",DistTable[MinDistRoom].Dist,DistTable[MinDistRoom].Time);    printf("路线:");    PrintPath(MinDistRoom,DistTable,G);    puts("");    printf("到达时间最快的教室:%s\n",G->AdList[MinTimeRoom].Name);    printf("行程:%.1lf \t花费时间:%.1lf\n",TimeTable[MinTimeRoom].Dist,TimeTable[MinTimeRoom].Time);    printf("路线:");    PrintPath(MinTimeRoom,TimeTable,G);    puts("");}int MenuSelect(void){    char Choice;    do{system("cls");printf("\t\t\t ========济南大学导航系统======== \n");printf("\t\t\t  ┌──────────────────┐ \n");printf("\t\t\t  │ 1. 导航          │ \n");printf("\t\t\t  │ 2. 学霸导航      │ \n");printf("\t\t\t  │ 0. 退出导航系统  │ \n");printf("\t\t\t  └──────────────────┘ \n");printf("\t\t\t ============================ \n");printf("\n\t\t请您选择(0-2):");Choice=getchar();}while(Choice<'0'||Choice>'2');    return (Choice-'0');}void TestTable(int n,Table T[]){    int i;    for(i=0;i<n;i++)    {        printf("%d:Known:%d Dist:%lf Time:%lf Path:%d\n",T[i].VetexId,T[i].Known,T[i].Dist,T[i].Time,T[i].Path);    }    puts("");}Graph CreatGraph(void){    int i,Cell1,Cell2;    double TmpDist,TmpTime;    Graph G;    char Tmpstr1[20],Tmpstr2[20];    Edge TmpEdge;    G=malloc(GraphLEN);    puts("input the Number of Vetex:");    scanf("%d",&G->NumVetex);    G->AdList=calloc(G->NumVetex,VetexNodeLEN);    puts("input the Number of Edge:");    scanf("%d",&G->NumEdge);    puts("Now Input The Vetex Information:");    puts("Every Place'Name Must Use Chinese Language Or Number!");    puts("");    for(i=0;i<G->NumVetex;i++)    {        G->AdList[i].NumEdge=0;        G->AdList[i].IsClassRoom=0;        G->AdList[i].Id=i;        do        {            printf("Input The No.%d Vetex's Name:",i+1);            scanf("%s",G->AdList[i].Name);        }while(!IsChinese(G->AdList[i].Name));        printf("Is This Vetex A ClassRoom?(1/0) :");        scanf("%d",&G->AdList[i].IsClassRoom);        G->AdList[i].Header=NULL;    }    puts("Now Input The Edge Information:");    puts("There Is Space Between Two Place's Name!");    puts("");    for(i=0;i<G->NumEdge;i++)    {        printf("No.%d Edge:Input Two Vetex's Name:",i+1);        scanf("%s %s",Tmpstr1,Tmpstr2);        Cell1=FindVetexIdBaseName(Tmpstr1,G);        if(strcmp(Tmpstr1,G->AdList[Cell1].Name)!=0)        {            puts("Not have this Vetex!,Input Again!");            i--;            continue;        }        Cell2=FindVetexIdBaseName(Tmpstr2,G);        if(strcmp(Tmpstr2,G->AdList[Cell2].Name)!=0)        {            puts("Not have this Vetex!,Input Again!");            i--;            continue;        }        G->AdList[Cell1].NumEdge++;G->AdList[Cell2].NumEdge++;        puts("Input the Distance Of Two Vetex:");        scanf("%lf",&TmpDist);        puts("Input the Time-Cost Of Two Vetex:");        scanf("%lf",&TmpTime);        TmpEdge=malloc(EdgeLEN);        TmpEdge->Dist=TmpDist;        TmpEdge->Time=TmpTime;        strcpy(TmpEdge->Name,G->AdList[Cell2].Name);        TmpEdge->VetexId=G->AdList[Cell2].Id;        TmpEdge->Next=G->AdList[Cell1].Header;        G->AdList[Cell1].Header=TmpEdge;/*Let Cell2 Vetex Linked Cell1 To Make A Edge*/        TmpEdge=malloc(EdgeLEN);        TmpEdge->Dist=TmpDist;        TmpEdge->Time=TmpTime;        strcpy(TmpEdge->Name,G->AdList[Cell1].Name);        TmpEdge->VetexId=G->AdList[Cell1].Id;        TmpEdge->Next=G->AdList[Cell2].Header;        G->AdList[Cell2].Header=TmpEdge;/*Let Cell1 Vetex Linked Cell1 To Make A Edge*/    }    return G;}void Test(void )/*Test Function*/{    int i;    Graph G;    char StarPlace[20];    char ArrivePlace[20];    int StartVetexId=18,ArriveVetexId=0;    Table *DistTable,*TimeTable;    G=ReadGraph();    DistTable=calloc(G->NumVetex,TableLEN);    TimeTable=calloc(G->NumVetex,TableLEN);    ShortestPath(DistTable,TimeTable,StartVetexId,G);    TestTable(G->NumVetex,DistTable);    TestTable(G->NumVetex,TimeTable);}int main(){    char Choice='y';    int IsContinue=0;    Graph G;    char StarPlace[20];    char ArrivePlace[20];    int StartVetexId,ArriveVetexId;    Table *DistTable,*TimeTable;    G=ReadGraph();    DistTable=calloc(G->NumVetex,TableLEN);    TimeTable=calloc(G->NumVetex,TableLEN);    for(;Choice=='y'||Choice=='Y';)    {        switch(MenuSelect())        {            case 1:            {                do                {                    system("cls");                    IsContinue=0;                    puts("你在哪?");                    printf(":");                    scanf("%s",StarPlace);                    StartVetexId=FindVetexIdBaseName(StarPlace,G);                    if(strcmp(StarPlace,G->AdList[StartVetexId].Name)!=0)                    {                        puts("地图上没有这个地点!请再次输入");                        IsContinue=1;                    }                }while(!IsChinese(StarPlace)||IsContinue);                IsContinue=0;                do                {                    IsContinue=0;                    puts("你要去哪?");                    printf(":");                    scanf("%s",ArrivePlace);                    ArriveVetexId=FindVetexIdBaseName(ArrivePlace,G);                    if(strcmp(ArrivePlace,G->AdList[ArriveVetexId].Name)!=0)                    {                        puts("地图上没有这个地点!请再次输入");                        IsContinue=1;                    }                }while(!IsChinese(ArrivePlace)||IsContinue);                ShortestPath(DistTable,TimeTable,StartVetexId,G);                system("cls");                printf("距离最短路线:\n");                printf("行程:%.1lf \t花费时间:%.1lf\n",DistTable[ArriveVetexId].Dist,DistTable[ArriveVetexId].Time);                printf("路线:");                PrintPath(ArriveVetexId,DistTable,G);                puts("");                printf("时间最快路线:\n");                printf("行程:%.1lf \t花费时间:%.1lf\n",TimeTable[ArriveVetexId].Dist,TimeTable[ArriveVetexId].Time);                printf("路线:");                PrintPath(ArriveVetexId,TimeTable,G);                getchar();                printf("\n继续导航吗?");                scanf("%c",&Choice);                break;            }            case 2:            {                system("cls");                do                {                    IsContinue=0;                    puts("学霸,你在哪?");                    printf(":");                    scanf("%s",StarPlace);                    StartVetexId=FindVetexIdBaseName(StarPlace,G);                    if(strcmp(StarPlace,G->AdList[StartVetexId].Name)!=0)                    {                        puts("地图上没有这个地点!请再次输入");                        IsContinue=1;                    }                }while(!IsChinese(StarPlace)||IsContinue);                system("cls");                ShortestPath(DistTable,TimeTable,StartVetexId,G);                NearestClassRoom(G,DistTable,TimeTable);                getchar();                printf("\n学霸,你还需要别的服务吗?");                scanf("%c",&Choice);                break;            }            case 0:            {                printf("\n\t\t\t感谢您的使用,再见!\n");                exit(0);            }        }    }    printf("\n\t\t\t感谢您的使用,再见!\n");    return 0;}我自己导入地图可以在我的网盘下载 用来测试 放在同一个文件夹里面即可 不用改名字
graph.dat

地图里面包含的地点名字:

西门 3食堂 运动场 大礼堂 体育场 1教 2教 梅花馆 学24 9食堂 2食堂 8食堂 学10 学11 学12 11教 10教 12教 图书馆 




0 0