本博文源于浙江大学的《数据结构》主要解决了有向图带权值的单源最短路径问题,采用了Dijkstra算法,内容是姥姥上课讲过的,我下面先把测试用例点名一下,文末附上完整代码:
博主懒惰,实现这个代码,大家应该知道答案的,距离为6,v0->v3->v6->v5,我都减去1了,大家应该都能明白的。
7 12
2 0 4
2 5 5
0 1 2
0 3 1
1 4 10
1 3 3
3 2 2
3 5 8
3 6 4
3 4 2
4 6 6
6 5 1
完整源码如下:
//实现有权图单源最短路径问题
//实现无权图单源最短路径邻接矩阵实现
#include<stdio.h>
#include<stdlib.h>
#define MaxVertexNum 10
#define false 0
#define true 1
#define INFINITY 65535
#define ERROR -65535
typedef int WeiGraphhtType;
typedef int DataType;
typedef struct GraphNode *PtrToGraphNode;
typedef int bool;
typedef int Position;
typedef int ElementType;
int Visited[MaxVertexNum]={false};
struct GraphNode {
int Nv; //一张图的顶点数
int Ne; //一张图的边数
WeiGraphhtType Graph[MaxVertexNum][MaxVertexNum];
DataType Data[MaxVertexNum];//存顶点的数据
};
typedef PtrToGraphNode MGraphraph;//以邻接矩阵存储的图类型
//初始化一个有VertexNum 个顶点但没有边的图
typedef int Vertex;//用顶点下标表示顶点,为整型
MGraphraph CreateGraphraph(int VertexNum)
{
Vertex V, W;
MGraphraph Graphraph;
Graphraph = (MGraphraph)malloc(sizeof(struct GraphNode));
Graphraph->Nv = VertexNum;
Graphraph->Ne = 0;
//注意:这里默认顶点编号从0开始,到(Graphraph->Nv-1)
for(V=0; V<Graphraph->Nv;V++)
for(W=0; W<Graphraph->Nv;W++)
Graphraph->Graph[V][W] = 0;
for(int i =0;i<Graphraph->Nv;i++)
{
Visited[i]=false;
Graphraph->Data[i]=i;
}
return Graphraph;
}
typedef struct ENode *PtrToENode;
struct ENode {
Vertex V1,V2;//有向边<V1,V2>
WeiGraphhtType WeiGraphht;//权值
};
typedef PtrToENode EdGraphe;
void InsertEdGraphe(MGraphraph Graphraph, EdGraphe E)
{
Graphraph->Graph[E->V1][E->V2] = E->WeiGraphht;
}
MGraphraph BuildGraphraph()
{
MGraphraph Graphraph;
EdGraphe E;
Vertex V;
int Nv,i;
scanf("%d",&Nv);
Graphraph = CreateGraphraph(Nv);
scanf("%d",&(Graphraph->Ne));
if(Graphraph->Ne != 0) {
E = (EdGraphe)malloc(sizeof(struct ENode));
for(i=0;i<Graphraph->Ne;i++) {
scanf("%d %d %d",&E->V1,&E->V2,&E->WeiGraphht);
InsertEdGraphe(Graphraph, E);
}
}
return Graphraph;
}
void DijkstraPath(MGraphraph Graph,int *dist,int *path,int v0) //v0表示源顶点
{
int i,j,k;
int visited[Graph->Nv];
for(i=0;i<Graph->Nv;i++) //初始化
{
if(Graph->Graph[v0][i]>0&&i!=v0)
{
dist[i]=Graph->Graph[v0][i];
path[i]=v0; //path记录最短路径上从v0到i的前一个顶点
}
else
{
dist[i]=INT_MAX; //若i不与v0直接相邻,则权值置为无穷大
path[i]=-1;
}
visited[i]=false;
path[v0]=v0;
dist[v0]=0;
}
visited[v0]=true;
for(i=1;i<Graph->Nv;i++) //循环扩展n-1次
{
int min=INT_MAX;
int u;
for(j=0;j<Graph->Nv;j++) //寻找未被扩展的权值最小的顶点
{
if(visited[j]==false&&dist[j]<min)
{
min=dist[j];
u=j;
}
}
visited[u]=true;
for(k=0;k<Graph->Nv;k++) //更新dist数组的值和路径的值
{
if(visited[k]==false&&Graph->Graph[u][k]>0&&min+Graph->Graph[u][k]<dist[k])
{
dist[k]=min+Graph->Graph[u][k];
path[k]=u;
}
}
}
}
void PrintPath(int path[],int pt)
{
int tmp = pt;
int cnt = 1;
int collected[MaxVertexNum] = {0};
while(path[tmp] != 0)
{
collected[cnt] = path[tmp];
cnt++;
tmp = path[tmp];
}
collected[cnt] = path[tmp];
while(cnt != 0){
printf("v%d->",collected[cnt]);
cnt --;
}
printf("v%d",pt);
}
int main()
{
MGraphraph Graphraph = BuildGraphraph();
int Nv = Graphraph->Nv;
int dist[Nv];
int path[Nv];
DijkstraPath(Graphraph,dist,path,0);
int distance = 5;
PrintPath(path,distance);
printf("\ncost distance :%d",dist[distance]);
return 0;
}