-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dijkstra_traval_planning_planning.c
170 lines (138 loc) · 3.57 KB
/
Dijkstra_traval_planning_planning.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#define MAXV 500
#define INFINITE 100000000
typedef struct
{
int dist[MAXV][MAXV]; //表示两点之间的距离
int cost[MAXV][MAXV]; //两点之间需要的费用
int visited[MAXV]; //某点是否已经被访问过
int mindist[MAXV]; //从源点到该点之间的距离
int mincost[MAXV]; //从源点到该点的最小费用
int VertexN; //顶点数
int EdgeN; //边数
}Graph;
void initialize_graph(Graph *G)
{
int i = 0;
int j = 0;
if(NULL == G)
{
printf("the graph is null\n");
return;
}
for(i=0;i<MAXV;i++)
{
for(j=0;j<MAXV;j++)
{
G->dist[i][j] = INFINITE;
G->cost[i][j] = INFINITE;
}
G->visited[i] = 0;
G->mindist[i] = INFINITE;
G->mincost[i] = INFINITE;
}
G->VertexN = 0;
G->EdgeN = 0;
return;
}
void read_graph(Graph *G,int *start,int *end)
{
int i = 0;
int V1 = 0;
int V2 = 0;
if(NULL == G || NULL == start || NULL == end)
{
printf("data error\n");
return;
}
scanf("%d %d %d %d",&G->VertexN,&G->EdgeN,start,end);
printf("the vertext number is %d,the edge number is %d\n",G->VertexN,G->EdgeN);
printf("the start is %d,the end is %d\n",*start,*end);
for(i=0;i< G->EdgeN;i++)
{
scanf("%d %d",&V1,&V2);
scanf("%d %d",&G->dist[V1][V2],&G->cost[V1][V2]);
G->dist[V2][V1] = G->dist[V1][V2]; //无向图,V1到V2的距离和V2到V1的距离是一的。
G->cost[V2][V1] = G->cost[V1][V2];
printf("%d %d dist is :%d cost is : %d",V1,V2,G->dist[V1][V2],G->cost[V1][V2]);
}
}
void Dijstra(Graph *G,int start)
{
int w = 0;
int v = 0;
int min_vertex = 0;
int min_dis = 0;
if(NULL == G)
{
printf("the graph pointer is null\n");
return;
}
if(start < 0 || start >= MAXV)
{
printf("the start point is out of range\n");
return;
}
G->visited[start] = 1;
G->mindist[start] = G->mincost[start] = 0;
for(v = 0; v< G->VertexN;v++)
{
G->mindist[v] = G->dist[start][v];
G->mincost[v] = G->cost[start][v];
}
for(w=1;w<G->VertexN;w++)
{
min_dis = INFINITE;
for(v=0;v<G->VertexN;v++)
{
if((G->visited[v] != 1) && (G->mindist[v] <= min_dis))
{
min_dis = G->mindist[v];
min_vertex = v;
}
}
if(min_dis < INFINITE)
{
G->visited[min_vertex] = 1;
}
else
{
break;
}
for(v=0;v<G->VertexN;v++)
{
if(G->visited[v] != 1)
{
if(G->mindist[min_vertex] + G->dist[min_vertex][v] < G->mindist[v])
{
G->mindist[v] = G->dist[min_vertex][v] + G->mindist[min_vertex];
G->mincost[v] = G->mincost[min_vertex] + G->cost[min_vertex][v];
}
else if((G->mindist[min_vertex]+G->dist[min_vertex][v] == G->mindist
[v]) && (G->mincost[min_vertex]+G->cost[min_vertex][v] < G->mincost[v]))
{
G->mincost[v] = G->mincost[min_vertex] + G->cost[min_vertex][v];
}
}
}
}
return;
}
int main(void)
{
int start_point = 0;
int end_point = 0;
Graph *G = NULL;
G = (Graph*)malloc(sizeof(Graph));
if(NULL == G)
{
return 0;
}
initialize_graph(G);
read_graph(G,&start_point,&end_point);
Dijstra(G,start_point);
printf("the end point is %d,the start point is %d\n",end_point,start_point);
printf("%d %d\n",G->mindist[end_point],G->mincost[end_point]);
return 0;
}