-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdijkstra.cpp
99 lines (78 loc) · 1.21 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <climits>
//#define INT_MAX 1000
using namespace std;
int m, n, a[100][100], x;
ifstream f("graf.txt");
void init_matrice(){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
//if(i == j){
// a[i][i] = 0;
//}
//else{
a[i][j] = INT_MAX;
//}
}
}
}
void citire(){
int x, y;
for(int i=0; i<m; i++){
f>>x>>y;
f>>a[x][y];
}
}
/* DIJKSTRA */
int s[100], d[100], t[100];
void init_dijkstra(int n, int x){
s[x] = 1;
for(int i=1; i<=n; i++){
d[i] = a[x][i];
if(a[x][i] != INT_MAX && a[x][i] != 0){
t[i] = x;
}
}
}
void dijkstra(int x){
int minim = INT_MAX, p;
for(int i=1; i<n; i++){
minim = INT_MAX;
for(int j=1; j<=n; j++){
if(!s[j] && d[j] < minim){
minim = d[j];
p = j;
}
}
s[p] = 1;
if(minim == INT_MAX){
return;
}
for(int j=1; j<=n; j++){
if(d[p] + a[p][j] < d[j] && a[p][j] != INT_MAX){
d[j] = d[p] + a[p][j];
t[j] = p;
}
}
}
}
void afisare(int x, int y){
if(t[y] != 0){
afisare(x, t[y]);
cout<<y<<' ';
}
else{
cout<<x<<' ';
}
}
int main(){
f>>n>>m;
init_matrice();
citire();
f>>x;
init_dijkstra(n, x);
dijkstra(x);
afisare(x, 1);
f.close();
}