-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdijkstra-m.cpp
More file actions
153 lines (146 loc) · 3.64 KB
/
dijkstra-m.cpp
File metadata and controls
153 lines (146 loc) · 3.64 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#define RANDOM_FILL 0
#define PRINT_BOX 0
#define UNORDERED_MAP 0
#include <iostream>
#include <iomanip>
#include <set>
#include <map>
#include <unordered_map>
#include <utility>
#include <string>
#include <climits>
#include <cstdlib>
#include <ctime>
using namespace std;
#if RANDOM_FILL
const unsigned start_point = 0, BOX_SIZE = 5, 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
#if UNORDERED_MAP
namespace std {
template<> struct hash<pair<unsigned, unsigned>> {
size_t operator()(const pair<unsigned, unsigned>& n) const {
return hash<unsigned>()(n.first | n.second << 16);
}
};
}
#endif
set<unsigned> S;
unsigned D[TOPS], arc_count[TOPS];
#if UNORDERED_MAP
unordered_map<pair<unsigned,unsigned>, unsigned> lengths;
#else
map<pair<unsigned,unsigned>, unsigned> lengths;
#endif
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[make_pair(v2, v1)] = lengths[make_pair(v1, v2)] = rand()%max_arc_length + 1;
}
}
}
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[make_pair(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[make_pair(v1*BOX_SIZE + v2, v1*BOX_SIZE + v2 + BOX_SIZE)]
<< setw(w1 + 2) << " ";
cout << setw(w2) << lengths[make_pair(v1*BOX_SIZE + v2, v1*BOX_SIZE + v2 + BOX_SIZE)] << endl;
}
}
}
#else
void fill_sets() {
unsigned i, v1, v2;
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[make_pair(v1, v2)] = ini_arcs[i][2];
//lengths[make_pair(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;
decltype(lengths.begin()) p;
#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
if ((p = lengths.find(make_pair(start_point, i))) != lengths.end())
D[i] = p->second;
else
D[i] = UINT_MAX;
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 ((p = lengths.find(make_pair(j, i))) != lengths.end()) {
l = p->second + D[j];
if (D[i] > l) {
D[i] = l;
path[i] = path[j] + "-" + to_string(i);
arc_count[i] = arc_count[j] + 1;
}
}
}
}