任务描述
本关任务:编程实现求图的最短路径
相关知识
最短路径的Dijkstra算法:
求最短路径就是求图中的每一个点到图中某一个给定点(认为编号为0的点)的最短距离。
具体算法就是初始有一个旧图,一个新图。开始时旧图中有所有的结点,新图中初始为只有一个结点(源点,路径的源头)。整个算法就是不停的从旧图中往新图中添加点,直到所有的点都添加到新图中为止。
编程要求
根据提示,在右侧编辑器补充代码,计算并输出图的最短路径
测试说明
平台会对你编写的代码进行测试:
测试输入:按照指导书上图的数据进行输入
预期输出:
(1^5) (2^3) (4^2)
(2^2) (3^6)
(1^1) (3^2)
(1^6) (2^10) (3^4)
0@0,1@4,2@3,3@5,4@2
#include <stdio.h>
#include <stdlib.h>
char infomation[20];
//输入图
//1 5 2 3 4 2:
//3 6 2 2:
//3 2 1 1:
//:
//1 6 2 10 3 4:
typedef struct{
int length;//最短路径长度
int prevex;//从V0到Vi的最短路径上Vi的前驱节点
}Path;
Path *dist;
int main(int argc, const char * argv[]) {
int number = 5;
int arcs[5][5]={
{0,5,3,65535,2},
{65535,0,2,6,65535},
{65535,1,0,2,65535},
{65535,65535,65535,0,65535},
{65535,6,10,4,0}
};
dist = (Path*)malloc(sizeof(Path)*number);
for(int i=0;i<number;i++){
for(int j=0;j<number;j++){
if(arcs[i][j]!=0&&arcs[i][j]!=65535){
printf("(%d^%d)",j,arcs[i][j]);
if(i==0&&j==4)printf(" ");
for(int k=j+1;k<number;k++)
if(arcs[i][k]!=0&&arcs[i][k]!=65535){
printf(" ");
break;
}
}
}
printf("\n");
}
//初始化指定顶点V0到V-U中各顶点的距离
int i;
dist[0].length=0;
dist[0].prevex=0;
arcs[0][0] = 1;
for(i=1;i<number;i++){
dist[i].length = arcs[0][i];
if(dist[i].length!=65535)dist[i].prevex = 0;
else dist[i].prevex = -1;
}
int j,mv;
int min;
for(i=1;i<number;++i){
min = 65535;mv=0;
for(j=1;j<number;++j)
if(arcs[j][j]==0&&dist[j].length<min){
mv = j;min=dist[j].length;
}
if(mv==0)break;
arcs[mv][mv]=1;
for(j=1;j<number;j++){
if(arcs[j][j]==0&&dist[j].length>dist[mv].length+arcs[mv][j]){
dist[j].prevex = mv;
dist[j].length = dist[mv].length + arcs[mv][j];
}
}
}
for(i = 0;i<number-1;i++)printf("%d@%d,",i,dist[i].length);
printf("%d@%d",i,dist[i].length);
return 0;
}
如果是写实验报告,可以用下面的代码
#include <stdio.h>
#include <stdlib.h>
char infomation[20];
//输入数据 5
//1 5 2 3 4 2:
//3 6 2 2:
//3 2 1 1:
//:
//1 6 2 10 3 4:
typedef struct{
int length;//最短路径长度
int prevex;//从V0到Vi的最短路径上Vi的前驱节点
}Path;
Path *dist;
int main(int argc, const char * argv[]) {
int number;
printf("Enter a network. The minimal weights of paths from vertex 0 to other vertices\n");
printf("How many vertices in the digraph (between 1 and 10)?");
scanf("%d",&number);
int arcs[number][number];
dist = (Path*)malloc(sizeof(Path)*number);
for(int i = 0;i<number;i++)
for (int j=0; j<number ; j++)
arcs[i][j]=0;
printf("For each vertex, give the vertices to which it points.\nand the corresponding directed edge weight.\nType the pairs of numbers (terminated by a : for each vertex), separated by blanks.\n");
for(int i=0;i<number;i++){
printf("Vertex %d : ",i);
fflush(stdin);
fgets(infomation,20,stdin);
int flag = 0;
char temp='0';
for(int j = 0;j<20;j++){
if(infomation[j]==' ')continue;
else if(infomation[j]==':')break;
if(flag==0){
temp = infomation[j];
flag = 1;
}else if(flag==1){
arcs[i][temp-'0'] = infomation[j]-'0';
if(infomation[j+1]!=' '&&infomation[j+1]!=':'){
arcs[i][temp-'0']*=10;
flag=3;
continue;
}
flag=0;
}else if(flag==3){
flag=0;
continue;
}
}
for(int j = 0;j<20;j++) infomation[j] = '0';
}
for(int i = 0;i<number;i++)
{
for (int j=0; j<number ; j++)
if(i==j)arcs[i][j]=0;
else if(arcs[i][j]==0)arcs[i][j]=65535;
}
printf("\nYou entered the graph:\n\n");
for(int i=0;i<number;i++){
printf("Vertex %d : ",i);
for(int j=0;j<number;j++){
if(arcs[i][j]!=0&&arcs[i][j]!=65535){
printf("<%d^%d> ",j,arcs[i][j]);
}
}
printf("\n");
}
//初始化指定顶点V0到V-U中各顶点的距离
int i;
dist[0].length=0;
dist[0].prevex=0;
arcs[0][0] = 1;
for(i=1;i<number;i++){
dist[i].length = arcs[0][i];
if(dist[i].length!=65535)dist[i].prevex = 0;
else dist[i].prevex = -1;
}
int j,mv;
int min;
for(i=1;i<number;++i){
min = 65535;mv=0;
for(j=1;j<number;++j)
if(arcs[j][j]==0&&dist[j].length<min){
mv = j;min=dist[j].length;
}
if(mv==0)break;
arcs[mv][mv]=1;
for(j=1;j<number;j++){
if(arcs[j][j]==0&&dist[j].length>dist[mv].length+arcs[mv][j]){
dist[j].prevex = mv;
dist[j].length = dist[mv].length + arcs[mv][j];
}
}
}
printf("The minimal distances from vertex O to the other vertices are as follows:\n");
for(i = 0;i<number;i++)printf("%d@%d, ",i,dist[i].length);
printf("\n");
return 0;
}
不得不说,学校出的头歌题目和实验报告都有点问题,已经向老师反馈了