-
Notifications
You must be signed in to change notification settings - Fork 0
/
mice_and_maze.cpp
118 lines (95 loc) · 2.82 KB
/
mice_and_maze.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <cstring>
#include <set>
#include <limits>
using namespace std;
//#define _DEBUG
#define CELL_MAX 101
#define INF 0x1F
const unsigned int infinity = 100000; // pay attention cause memset takes chars and not ints //numeric_limits<unsigned int>::max();
unsigned int cells, exit_cell, timer;
unsigned int dist[CELL_MAX][CELL_MAX];
unsigned int dijkstra(unsigned int entry) {
unsigned int min_dist[CELL_MAX];
<<<<<<< HEAD
memset(min_dist, INF, sizeof min_dist);
=======
memset(min_dist, infinity, CELL_MAX);
>>>>>>> bbe4f0e94e5c82cb3a08b2be9bf466afd7abd4e8
min_dist[entry] = 0;
#ifdef _DEBUG
for(auto a : min_dist)
cout << a << " ";
#endif
// group of unvisited cells
set<unsigned int> cells_not_visited;
for (unsigned int i = 1; i <= cells; i++)
cells_not_visited.insert(i);
while (1) {
// get nearest unvisited cell
<<<<<<< HEAD
unsigned int nearest_cell = 0, nearest_distance = 0x1f1f1f1f;
=======
unsigned int nearest_cell = 0, nearest_distance = infinity;
>>>>>>> bbe4f0e94e5c82cb3a08b2be9bf466afd7abd4e8
for (unsigned int i = 1; i <= cells; i++)
if (min_dist[i] < nearest_distance &&
cells_not_visited.find(i) != cells_not_visited.end()) {
nearest_cell = i;
nearest_distance = min_dist[i];
}
if (nearest_cell == exit_cell) {
#ifdef _DEBUG
cout << "rat in cell " << entry << " : " << nearest_distance << endl;
#endif
return nearest_distance;
}
// visit this cell
<<<<<<< HEAD
for (unsigned int i = 1; i <= cells; i++)
=======
#ifdef _DEBUG
cout << "visit " << nearest_cell << endl << endl;
#endif
for (unsigned int i = 1; i <= cells; i++) {
>>>>>>> bbe4f0e94e5c82cb3a08b2be9bf466afd7abd4e8
if (min_dist[nearest_cell] + dist[nearest_cell][i] < min_dist[i])
min_dist[i] = min_dist[nearest_cell] + dist[nearest_cell][i];
#ifdef _DEBUG
cout << "min_dist cell " << i << " = " << min_dist[i] << endl;
#endif
}
cells_not_visited.erase(nearest_cell);
}
}
int main() {
int cases;
cin >> cases;
int cases2 = cases;
for ( ; cases > 0; cases--) {
unsigned int connections;
cin >> cells >> exit_cell >> timer >> connections;
<<<<<<< HEAD
memset(dist, INF, sizeof dist); // set infinite distance
=======
memset(dist, infinity, CELL_MAX*CELL_MAX);
>>>>>>> bbe4f0e94e5c82cb3a08b2be9bf466afd7abd4e8
for ( ; connections > 0; connections--) {
unsigned int a, b, distance;
cin >> a >> b >> distance;
dist[a][b] = distance;
}
unsigned int successes = 0;
for (unsigned int i = 1; i <= cells; i++)
if (dijkstra(i) <= timer)
successes++;
<<<<<<< HEAD
if (cases != cases2)
cout << endl;
cout << successes << endl;
=======
cout << successes << endl << endl;
>>>>>>> bbe4f0e94e5c82cb3a08b2be9bf466afd7abd4e8
}
return 0;
}