-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
136 lines (130 loc) · 3.22 KB
/
dijkstra.cpp
File metadata and controls
136 lines (130 loc) · 3.22 KB
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
#define RANDOM_FILL 0
#define PRINT_BOX 0
#include <iostream>
#include <iomanip>
#include <set>
#include <string>
#include <climits>
#include <cstdlib>
#include <ctime>
using namespace std;
#if RANDOM_FILL
const unsigned start_point = 0, BOX_SIZE = 200, TOPS = BOX_SIZE*BOX_SIZE, end_point = TOPS - 1, max_arc_length = 9;
#else
const unsigned ARCS = 11, TOPS = 6, start_point = 0, end_point = 5;
unsigned ini_arcs[ARCS][3] = /* start, end, length */
{{1,2,4},
{1,3,11},
{2,3,5},
{2,4,2},
{2,5,8},
{2,6,7},
{3,5,5},
{3,6,3},
{4,5,1},
{4,6,10},
{5,6,3}};
#endif
set<unsigned> S;
unsigned (*lengths)[TOPS], D[TOPS], arc_count[TOPS];
string path[TOPS]; //path printed
#if RANDOM_FILL
void fill_random_box() {
unsigned v1, v2;
srand(time(0)%777);
for (v1 = 0; v1 < TOPS; ++v1) {
S.insert(v1);
for (v2 = 0; v2 < TOPS; ++v2)
if (v1 < v2) {
if (v1 + 1 == v2 && v2%BOX_SIZE != 0 || v1 + BOX_SIZE == v2)
lengths[v2][v1] = lengths[v1][v2] = rand()%max_arc_length + 1;
else
lengths[v2][v1] = lengths[v1][v2] = UINT_MAX;
}
}
}
void print_box() {
unsigned v1, v2, w1 = 1, w2 = 2;
/* for (v1 = 0; v1 < TOPS; ++v1)
for (v2 = 0; v2 < TOPS; ++v2)
if (v1 != v2)
cout << v1 << '-' << v2 << ' ' << lengths[v1][v2] << endl;*/
v1 = TOPS;
while ((v1 /= 10) > 0) ++w1;
v1 = max_arc_length;
while ((v1 /= 10) > 0) ++w2;
for (v1 = 0; v1 < BOX_SIZE; ++v1) {
for (v2 = 0; v2 < BOX_SIZE - 1; ++v2)
cout << '[' << setw(w1) << v1*BOX_SIZE + v2 << ']' << setw(w2)
<< lengths[v1*BOX_SIZE + v2][v1*BOX_SIZE + v2 + 1];
cout << '[' << setw(w1) << v1*BOX_SIZE + v2 << "]\n";
if (v1 < BOX_SIZE - 1) {
for (v2 = 0; v2 < BOX_SIZE - 1; ++v2)
cout << setw(w2) << lengths[v1*BOX_SIZE + v2][v1*BOX_SIZE + v2 + BOX_SIZE]
<< setw(w1 + 2) << " ";
cout << setw(w2) << lengths[v1*BOX_SIZE + v2][v1*BOX_SIZE + v2 + BOX_SIZE] << endl;
}
}
}
#else
void fill_sets() {
unsigned i, v1, v2;
for (v1 = 0; v1 < TOPS; ++v1)
for (v2 = 0; v2 < TOPS; ++v2)
lengths[v1][v2] = UINT_MAX;
for (i = 0; i < ARCS; ++i) {
v1 = ini_arcs[i][0] - 1;
v2 = ini_arcs[i][1] - 1;
S.insert(v1);
S.insert(v2);
lengths[v1][v2] = ini_arcs[i][2];
//lengths[v2][v1] = ini_arcs[i][2];
}
}
#endif
unsigned minD() {
unsigned min, index_min;
min = UINT_MAX;
for (auto i: S)
if (min > D[i]) {
min = D[i];
index_min = i;
}
return index_min;
}
int main() {
unsigned j, l;
lengths = (unsigned(*)[TOPS]) new unsigned [TOPS*TOPS];
#if RANDOM_FILL
fill_random_box();
#if PRINT_BOX
print_box();
#endif
#else
fill_sets();
#endif
D[start_point] = 0;
S.erase(start_point); //Step 1
for (auto i: S) { //Step 2
D[i] = lengths[start_point][i];
path[i] = to_string(start_point) + "-" + to_string(i);
arc_count[i]++;
}
for(;;) { //Step 3
j = minD();
S.erase(j);
if (j == end_point) { //Step 4
cout << "min = " << D[j] << " (" << arc_count[end_point] << ")\npath = " << path[end_point] << endl;
return 0;
}
for (auto i: S) //Step 5
if ((l = lengths[j][i]) != UINT_MAX) {
l += D[j];
if (D[i] > l) {
D[i] = l;
path[i] = path[j] + "-" + to_string(i);
arc_count[i] = arc_count[j] + 1;
}
}
}
}