Dijkstra算法

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define N 999999
struct city
{
char name[20];
};
struct graph
{
city cities[30];
int v,e;
int weight[30][30];
};


void djs(graph *G,int a,int b)
{
int i,j,k,t;
int s[29];
int p[29];
int d[29][29];
int path[29];
for(i=0;i<29;i++)
{s[i]=0;}
s[a]=1;
for(i=0;i<29;i++)
for(j=0;j<29;j++)
{d[i][j]=G->weight[i][j];}
for(i=0;i<29;i++)
{p[i]=a;}


for(i=0;i<29;i++)
{
//k a到其他节点最短距离
//t 当前最短节点
k=N;
for(j=0;j<29;j++){
{if(s[j]==0&&d[a][j]<k){k=d[a][j];t=j;}}//if<k
}//for
s[t]=1;
if(t==b)break;
for(j=0;j<29;j++)
{
if( d[a][j] > (d[a][t]+d[t][j]) ){
d[a][j] = d[a][t]+d[t][j];
p[j] = t;
}
}
}
if(d[a][b]<N)
{
printf("%s与%s之间最短路径长度为:%d\n",G->cities[a].name,G->cities[b].name,d[a][b]);
    i=b;
j=0;
while(i!=a)
{
path[j]=i;
j++;
i=p[i];
}
    printf("路径为:%s",G->cities[a].name);
for(i=j-1;i>-1;i--)printf("-->%s",G->cities[path[i]].name);
printf("\n");
}
else printf("无法到达.\n");
}




void main()
{
graph G;
G.v=29;
strcpy(G.cities[0].name,"北京");
strcpy(G.cities[1].name,"哈尔滨");
strcpy(G.cities[2].name,"沈阳");
strcpy(G.cities[3].name,"天津");
strcpy(G.cities[4].name,"济南");
strcpy(G.cities[5].name,"连云港");
strcpy(G.cities[6].name,"徐州");
strcpy(G.cities[7].name,"南京");
strcpy(G.cities[8].name,"上海");
strcpy(G.cities[9].name,"杭州");
strcpy(G.cities[10].name,"南昌");
strcpy(G.cities[11].name,"九龙");
strcpy(G.cities[12].name,"广州");
strcpy(G.cities[13].name,"长沙");
strcpy(G.cities[14].name,"武汉");
strcpy(G.cities[15].name,"株洲");
strcpy(G.cities[16].name,"柳州");
strcpy(G.cities[17].name,"郑州");
strcpy(G.cities[18].name,"包头");
strcpy(G.cities[19].name,"乌鲁木齐");
strcpy(G.cities[20].name,"兰州");
strcpy(G.cities[21].name,"宝鸡");
strcpy(G.cities[22].name,"焦作");
strcpy(G.cities[23].name,"西安");
strcpy(G.cities[24].name,"成都");
strcpy(G.cities[25].name,"重庆");
strcpy(G.cities[26].name,"昆明");
strcpy(G.cities[27].name,"贵阳");
strcpy(G.cities[28].name,"石家庄");
int i,j;
for(i=0;i<G.v;i++)
for(j=0;j<G.v;j++)
{G.weight[i][j]=N;}
G.weight[0][28]=420;
G.weight[0][3]=120;
G.weight[0][10]=2400;
G.weight[0][18]=450;
G.weight[1][2]=540;
G.weight[2][3]=600;
G.weight[3][4]=400;
G.weight[4][6]=500;
G.weight[5][6]=400;
G.weight[6][7]=500;
G.weight[6][17]=900;
G.weight[7][8]=500;
G.weight[8][9]=250;
G.weight[9][10]=600;
G.weight[10][11]=900;
G.weight[10][15]=450;
G.weight[11][12]=250;
G.weight[12][15]=600;
G.weight[13][14]=350;
G.weight[14][17]=600;
G.weight[15][27]=1100;
G.weight[16][22]=2000;
G.weight[17][23]=600;
G.weight[17][28]=500;
G.weight[18][20]=2000;
G.weight[19][20]=1700;
G.weight[20][21]=350;
G.weight[21][23]=500;
G.weight[21][24]=600;
G.weight[24][25]=600;
G.weight[24][26]=900;
G.weight[25][27]=600;
G.weight[26][27]=800;
for(i=0;i<G.v;i++)
for(j=0;j<G.v;j++)
{if(G.weight[i][j]!=N)G.weight[j][i]=G.weight[i][j];}
for(j=0;j<G.v;j++)
{G.weight[j][j]=0;}
int choose=1;
int a,b;
while(choose!=0)
{
system("cls");
printf("1城市间铁路查询 0退出\n");
scanf("%d",&choose);
if(choose==1){
printf("城市列表:\n");
for(int i=0;i<G.v;i++)
{printf("%d.%s ",i,G.cities[i].name);
if((i+1)%5==0)printf("\n");}
printf("\n输入起点和目的地城市编号:");
scanf("%d",&a);
scanf("%d",&b);
if(a<0||a>28||b<0||b>28)printf("城市不存在.\n");
else djs(&G,a,b);
}
system("pause");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值