//ShortestPath_DIJ.cpp
# include <iostream.h>
# include <stdio.h>
# include <malloc.h>
# include <conio.h>
# define INFINITY 1000
# define MAX_VERTEX_NUM 20
# define OK 1
# define ERROR 0
# define FALSE 0
# define TRUE 1
typedef enum{DG,DN,UDG,UDN} GraphKind;
typedef int EType;
typedef int InfoType;
typedef int VertexType;
typedef int PathMatrix;
typedef int ShortPathTable;
typedef struct ArcCell
{ EType adj;
InfoType info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{ VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
int CreatUDN(MGraph &G) //CreatUDN() function
{ int i=0,j=0,k,vi,vj,w;
cout<<"Please input the number of G.vexnum (eg,G.vexnum=6) : ";
cin>>G.vexnum;
cout<<"Please input the number of G.arcnum (eg,G.arcnum=8) : ";
cin>>G.arcnum;
for(i=0;i<G.vexnum;++i)
for(j=0;j<G.vexnum;++j)
{ G.arcs[i][j].info=INFINITY;
}
cout<<"Plese input arc(Vi-->Vj):"<<endl
<<"For example:"<<endl<<"------------"<<endl
<<"(Vi=1,Vj=6,Weight=100),(Vi=1,Vj=5,Weight=30),(Vi=1,Vj=3,Weight=10)"<<endl
<<"(Vi=2,Vj=3,Weight=5),(Vi=3,Vj=4,Weight=50),(Vi=4,Vj=6,Weight=10)"<<endl
<<"(Vi=5,Vj=6,Weight=60),(Vi=5,Vj=4,Weight=20)..."<<endl;
for(k=0;k<G.arcnum;++k)
{ cout<<endl<<"Please input the "<<k+1<<"th arc's vi (0<vi<"<<G.vexnum+1<<"): ";
cin>>vi;
cout<<"Please input the "<<k+1<<"th arc's vj (0<vj<"<<G.vexnum+1<<"): ";
cin>>vj;
cout<<"Please input the "<<k+1<<"th arc's weight (0<weight<"<<INFINITY<<"): ";
cin>>w;
i=vi;
j=vj;
while(i<1||i>G.vexnum||j<1||j>G.vexnum||w<0||w>=INFINITY)
{
cout<<"Input ERROR!"<<endl
<<"Please input the "<<k+1<<"th arc's vi (0<vi<"<<G.vexnum+1<<"): ";
cin>>vi;
cout<<"Please input the "<<k+1<<"th arc's vj (0<vj<"<<G.vexnum+1<<"): ";
cin>>vj;
cout<<"Please input the "<<k+1<<"th arc's weight (0<weight<1000): ";
cin>>w;
i=vi;
j=vj;
}//end of while
i--;
j--;
G.arcs[i][j].info=w;
}//end of for(k=0;k<G.arcnum...)
return (OK);
}//end CreatVDN() function
void ShortestPath_DIJ(MGraph G,int v0,PathMatrix Path[MAX_VERTEX_NUM][MAX_VERTEX_NUM],ShortPathTable Dist[MAX_VERTEX_NUM])
{
int i,j,v,w,min,final[MAX_VERTEX_NUM];
for(v=0;v<G.vexnum;++v)
{ final[v]=FALSE;
Dist[v]=G.arcs[v0][v].info;
for(w=0;w<G.vexnum;++w)
Path[v][w]=FALSE;
if(Dist[v]<INFINITY)
{ Path[v][v0]=TRUE;
Path[v][v]=TRUE;
} //end of if
}//end of for(v=0;v<G.vexnum...)
Dist[v0]=0;
final[v0]=TRUE;
printf("Vertice");
for(i=0;i<G.vexnum;i++)
printf("%5d",i+1);
printf("\n");
printf("Step %d :",v0+1);
for(i=0;i<G.vexnum;i++)
printf("%5d",Dist[i]);
printf("\n");
for(i=1;i<G.vexnum;++i) //Do the rest of G.vexnum-1 node
{ min=INFINITY;
for(w=0;w<G.vexnum;++w)
if(!final[w])
if(Dist[w]<min)
{ v=w;
min=Dist[w];
}//end of if
final[v]=TRUE;
printf("Step %d :",i+1);
for(w=0;w<G.vexnum;++w)
{if(!final[w]&&(min+G.arcs[v][w].info<Dist[w]))
{ Dist[w]=min+G.arcs[v][w].info;
for(j=0;j<G.vexnum;j++)
Path[w][j]=Path[v][j];
Path[w][w]=TRUE;
}//end of if
printf("%5d",Dist[w]);
}//end of for(w=0;w<G.vexnum;++w)
printf("\n");
}//end of for(i=1;,,,)
}//ShortPath_DIJ();
void main() //main() function
{ MGraph G;
int v0=0; //modify
int i,j;
PathMatrix Path[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
ShortPathTable Dist[MAX_VERTEX_NUM];
cout<<"ShortestPath_DIJ.cpp"<<endl<<"===================="<<endl<<endl;
if(CreatUDN(G))
cout<<endl<<"Create MGraph success !"<<endl<<"InitMGraph are Show:"<<endl<<"--------------------"<<endl;
printf("Vertice");
for(i=0;i<G.vexnum;i++)
printf("%5d",i+1);
printf("\n");
for(i=0;i<G.vexnum;i++)
{ printf("%5d ",i+1);
for(j=0;j<G.vexnum;j++)
printf("%5d",G.arcs[i][j].info);
printf("\n");
}//end of for(i=0;i<G.vexnum;i++)
cout<<endl<<"By Dijkstra Algorithm:"<<endl<<"----------------------"<<endl;
ShortestPath_DIJ(G,v0,Path,Dist);
cout<<endl<<"...OK!..."<<endl;
getch();
}//end of main() function
ShortestPath_DIJ.rar_shortestpath
版权申诉
37 浏览量
2022-09-24
12:30:16
上传
评论
收藏 2KB RAR 举报
局外狗
- 粉丝: 64
- 资源: 1万+
最新资源
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.dta
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.xlsx
- Reeds+Shepp曲线算法讲解和实现.pdf
- 毕业设计基于SpringBoot+MyBatisPlus+MySQL+Vue的外卖配送信息系统源代码+数据库
- 词向量(Word Embeddings)是自然语言处理(NLP)领域的一种重要技术.txt
- Surfer,线性函数
- MyBatis 的动态 SQL 是其核心特性之一.txt
- 时代的sdddsddsddsd
- 基于哈希链表的简单人员信息管理系统
- 其他类别JdonFramework开源框架 v5.1 Build20071025-jdonframework-5.1.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈