单源最短路径的算法求从任意指定的一个城市出发,到过所有其它城市的一条代价最小(距离最短)的路线

图的应用

  • 1.题目描述
    已知中国12个城市及它们的经纬度:北京 北纬39.55 东经116.24 、兰州 北纬36.04 东经103.51 、广州 北纬23.08 东经113.14 、厦门 北纬24.27 东经118.06 、桂林 北纬25.17 东经110.17 、遵义 北纬27.42 东经106.55 、济南 北纬36.40 东经117.00 、郑州 北纬34.46 东经113.40 、哈尔滨 北纬45.44 东经126.36 、武汉 北纬30.35 东经114.17 、长沙 北纬28.12 东经112.59 、呼和浩特 北纬40.48 东经111.41。其中,北京至兰州,北京至桂林,北京至武汉,北京至长沙,桂林至广州,桂林至厦门,兰州至厦门,兰州至哈尔滨,广州至遵义,广州至哈尔滨,厦门至呼尔浩特,呼尔浩特至郑州,郑州至济南,遵义至长沙,遵义至武汉之间有交通线路,请用单源最短路径的算法求出从任意指定的一个城市出发,到过所有其它城市的一条代价最小(距离最短)的路线。
  • 2 .代码如下
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAXSIZE 20
#define CITYNUMBER 12
#define ROADNUMBER 15

typedef struct node{
    int adjvex;
    float weight;
    struct node *next;
} edgenode;

typedef struct{
    char city_name[MAXSIZE];
    float location[2];
} cityinformation;

typedef struct {
    cityinformation data;
    edgenode*link;
} citynode;

typedef struct {
    int flag;
    int last_city;
    float distant;
} city;


Char cities[CITYNUMBER][MAXSIZE]={{“北京”},{“兰州”},{“广州”},{“厦门”},{“桂林”},{“遵义”},{“济南”},{“郑州”},{“哈尔滨”},{“武汉”},{“长沙”},{“呼和浩特”}
};
float location[12][2]={{39.55,116.24},{36.04,103.51},{23.08,113.14},{24.27,118.06},{25.17,110.17},{27.42,106.55},{36.40,117.00},{34.46,113.40},{45.44,126.36},{30.35,114.17},{28.12,112.59},{40.48,111.41}
};
int connection[15][2]={{0,1},{0,4},{0,9},{0,10},{4,2},{4,3},{1,3},{1,8},{2,5},{2,8},{3,11},{11,7},{7,6},{5,10},{5,9}
};


double CountWeight(cityinformation ,cityinformation);
void CreateAdjList(citynode G[]);
void FindShortestWay(citynode G[]);
void __OutputResult__(city [],citynode []);


int main(int argc, const char * argv[]) {
    citynode G[CITYNUMBER];
    CreateAdjList(G);
    FindShortestWay(G);
    return 0;
}

void CreateAdjList(citynode G[]){
    int start,end;
    edgenode *tmp;
    for (int I=0;i<CITYNUMBER;i++){
        strcpy(G[I].data.city_name,cities[I]);
        G[I].data.location[0]=location[I][0];
        G[I].data.location[1]=location[I][1];
        G[I].link=NULL;
    }
    for (int j=0; j<ROADNUMBER; j++){
       
        start=connection[j][0];
        end=connection[j][1];
        int weight=CountWeight(G[start].data, G[end].data);
        tmp=(edgenode*)malloc(sizeof(edgenode));
        tmp->adjvex=end;
        tmp->weight=weight;
        tmp->next=G[start].link;
        G[start].link=tmp;
        
        tmp=(edgenode*)malloc(sizeof(edgenode));
        tmp->adjvex=start;
        tmp->weight=weight;
        tmp->next=G[end].link;
        G[end].link=tmp;
        
    }
}

double CountWeight(cityinformation a,cityinformation b){
    double result;
    result =sqrt((a.location[0]-b.location[0])*(a.location[0]-b.location[0])+(a.location[1]-b.location[1])*(a.location[1]-b.location[1]));
    return result;
}


void FindShortestWay(citynode G[]){
    city tmp [CITYNUMBER];
    char start_cityname[MAXSIZE];
    int start_city_id;
    printf(“请输入起点城市名:”);
    scanf(“%s”,start_cityname);
    for (int i=0;i<CITYNUMBER;i++){
        if(!strcmp(G[I].data.city_name, start_cityname)){
            start_city_id=I;
            tmp[I].flag=1;
            tmp[I].distant=0;
            tmp[I].last_city=-1;
        }else{
        tmp[I].flag=0;
        tmp[I].distant=9999;
        }
    }
    edgenode*a = G[start_city_id].link;
    while (a){
        tmp[a->adjvex].distant = a->weight;
        tmp[a->adjvex].last_city = start_city_id;
        a=a->next;
    }//初始化完成
    for(int I=0;i<CITYNUMBER-1;i++){
        int min=10000,next_shortest_city_id;
        for (int j=0; j<CITYNUMBER; j++) {
            if((!tmp[j].flag)&&(tmp[j].distant<min)){
                min = tmp[j].distant;
                next_shortest_city_id = j;
            }
        }
        tmp[next_shortest_city_id].flag = 1;
        edgenode*a = G[next_shortest_city_id].link;
        while (a) {
            if(!tmp[a->adjvex].flag && tmp[a->adjvex].distant>tmp[next_shortest_city_id].distant+a->weight){
                tmp[a->adjvex].distant = tmp[next_shortest_city_id].distant+a->weight;
                tmp[a->adjvex].last_city = next_shortest_city_id;
            }
            a=a->next;
        }
    }
    __OutputResult__(tmp, G);
}

void __OutputResult__(city a[],citynode b[]){
    int tmp[CITYNUMBER];
    for(int i=0;i<CITYNUMBER;i++){
        int count = 0;
        int j=I;
        while (a[j].last_city!=-1) {
            tmp[count++]=j;
            j=a[j].last_city;
        }
        tmp[count]=j;
        int k;
        for(k=count;k>0;k—){
            printf(“%s->”,b[tmp[k]].data.city_name);
        }
        if(count)
            printf(“%s\n”,b[tmp[k]].data.city_name);
    }
}

*3.运行结果

posted @ 2019-01-09 11:33  --TNT--  阅读(933)  评论(0)    收藏  举报