-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
48 lines (43 loc) · 780 Bytes
/
dijkstra.cpp
File metadata and controls
48 lines (43 loc) · 780 Bytes
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
#include <stdio.h>
#include "constant.h"
static double d[MAX];//distance
static int Free[MAX];
void init_d(int trace[], int n, int S)
{
for (int i=1; i<=n; i++) d[i]=MAXC;
d[S]=0;
for (int i=1; i<=n; i++) Free[i]=1;//all vertices is free
}
void Dijkstra(double *ShD, int trace[], pGraph graph, int S, int F)
{
int n=graph->numVertices;
int u,v;
double min;
pNode temp;
init_d(trace,n,S);
do
{
u=0; min=MAXC;
for (v=1; v<=n; ++v)
if (Free[v] && d[v]<min)
{
min=d[v];
u=v;
}
if (u==0 || u==F) break;
Free[u]=0;//fix vertex
temp=graph->lists[u]->next;
while (temp!=NULL)
{
v=temp->vertex;
if (Free[v] && (d[v]>d[u]+temp->cost))
{
d[v]=d[u]+temp->cost;
trace[v]=u;
}
temp=temp->next;
}
}
while (1);
*ShD=d[F];
}