-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.c
92 lines (90 loc) · 2.26 KB
/
Dijkstra.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<stdio.h>
#include <stdlib.h>
//单源最短路径
#define true 1
#define false 0
#define INFINITY 65535
#define ERROR 0
#define MaxVertexNum 100
typedef int DataType;
typedef int Vertex;
typedef int bool;
typedef int WeightType;
typedef struct GNode *PtrToGNode;
typedef struct GNode {
WeightType G[MaxVertexNum][MaxVertexNum];
DataType Data[MaxVertexNum];
int Nv,Ne;
};
typedef PtrToGNode MGraph;
Vertex FindMinDist(MGraph Graph,WeightType Dist[],bool collected[]) {
Vertex v,MinV;
WeightType MinDist=INFINITY;
for(v=0; v<Graph->Nv; v++) {
if(collected[v]==false && Dist[v]<MinDist) { //没有被收录,而且去权值更小
MinDist=Dist[v];
MinV=v;
}
}
if(MinDist<INFINITY) {
return MinV;
} else {
return ERROR;
}
}
bool Dijkstra(MGraph Graph,WeightType dist[], bool collected[],/*单源最短路径中的源*/ Vertex s,Vertex path[]){
Vertex v,w;//初始化路径数组与收集数组
for(v=0; v<Graph->Nv; v++) {
dist[v]=Graph->G[s][v];
collected[v]=false;
path[v]=-1;
}
collected[s]=true;//源已收集
dist[s]=0;//源已收集
while(1) {
v=FindMinDist(Graph,dist,collected);
if(!v) {
break;
}
collected[v]=true;
for(w=0; w<Graph->Nv; w++) {
if(collected[w]==false&&Graph->G[v][w]<INFINITY) {
if(Graph->G[v][w]<0)//出错:dist[W]<0
return false;
if(dist[w]>dist[v]+Graph->G[v][w]) {
dist[w]=dist[v]+Graph->G[v][w];
path[w]=v;
}
}
}
}
return true;
}
int main() {
int vertexnum,v,w,res,s;
Vertex path[MaxVertexNum];
WeightType Dist[MaxVertexNum];
bool collected[MaxVertexNum];
MGraph Graph;
Graph=(PtrToGNode)malloc(sizeof(struct GNode));
for(v=0;v<MaxVertexNum;v++){
collected[v]=false;
}
printf("*********************************请输入图的顶点个数********************************\n");
scanf("%d",&vertexnum);
Graph->Nv=vertexnum;
printf("*********************************请输入图的邻接矩阵********************************\n");
for(v=0; v<vertexnum; v++) {
for(w=0; w<vertexnum; w++) {
scanf("%d",&(Graph->G[v][w]));
}
}
printf("**************************************请输入源*************************************\n");
scanf("%d",&s);
res=Dijkstra(Graph,Dist,collected,s,path);
if(res==1)
printf("OK");
else
printf("ERROR");
return 0;
}