-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.cpp
75 lines (74 loc) · 1.5 KB
/
Dijkstra.cpp
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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <dos.h>
#define MAX 50
#define TRUE 1
#define FALSE 0
int n, s, t;
char chon;
int truoc[MAX], d[MAX], CP[MAX][MAX];
int final[MAX];
void Init(void){
FILE * fp;int i, j;
fp = fopen("DOTHI_Dijkstra.in","r");
fscanf(fp,"%d", &n);
printf("\n So dinh :%d",n);
printf("\n Ma tran khoang cach:");
for(i=1; i<=n;i++){
printf("\n");
for(j=1; j<=n;j++){
fscanf(fp, "%d", &CP[i][j]);
printf("%3d",CP[i][j]);
if(CP[i][j]==0) CP[i][j]=32000;
}
}
fclose(fp);
}
void Result(void){
int i,j;
printf("\n Duong di ngan nhat tu %d den %d la\n", s,t);
printf("%d<=",t);
i=truoc[t];
while(i!=s){
printf("%d<=",i);
i=truoc[i];
}
printf("%d",s);
printf("\n Do dai duong di la:%d", d[t]);
getch();
}
void Dijkstra(void){
int v, u, minp;
printf("\n Tim duong di tu s=");scanf("%d", &s);
printf(" den ");scanf("%d", &t);
for(v=1; v<=n; v++){
d[v]=CP[s][v];
truoc[v]=s;
final[v]=FALSE;
}
truoc[s]=0; d[s]=0;final[s]=TRUE;
while(!final[t]) {
minp=2000;
for(v=1; v<=n; v++){
if((!final[v]) && (minp>d[v]) ){
u=v;
minp=d[v];
}
}
final[u]=TRUE;// u- la dinh co nhan tam thoi nho nhat
if(!final[t]){
for(v=1; v<=n; v++){
if ((!final[v]) && (d[u]+ CP[u][v]< d[v])){
d[v]=d[u]+CP[u][v];
truoc[v]=u;
}
}
}
}
}
int main(void){
Init(); Dijkstra();
Result(); getch();
}