#include <bits/stdc++.h> using namespace std; typedef long double T; #define st first #define nd second #define PII pair <int, int> const int N = 200'007; const T INF = 1'000'007; const T epsilon = 1e-15; int n, v[4]; char in[4][N]; T ans = INF; int best_car[4]; bool can_finish[4][N]; T dist[4][N]; bool Used[4][4][N]; set <int> Free[4]; priority_queue < tuple <T, int, int> > Q; void update_ans(int x, T y, T d) { T cand_ans = d; for (int p = 1; p <= 3; ++p) { if (p == x || best_car[p] == 0) { continue; } T diff = v[p] * d + best_car[p] + 1 - v[x] * d - y; if (diff < 0) { continue; } cand_ans = max(cand_ans, d + diff / (v[0] - v[p])); } // printf("%d %.10Lf %.10Lf -> %.10Lf\n", x, y, d, cand_ans); ans = min(ans, cand_ans); } void check_and_push(int x, int y, T d) { if (dist[x][y] > d) { // printf("pushes %d %d with %.10Lf\n", x, y, d); dist[x][y] = d; Q.push({-d, x, y}); } } void first_move(int x, int y, int goal_x) { const T &d = dist[x][y]; T respective_y = y + d * (v[x] - v[goal_x]); int ly = floor(respective_y + epsilon); int ry = ceil(respective_y - epsilon); if (ly >= n) { update_ans(goal_x, respective_y, d); return; } if (ly < 1) { return; } if (in[goal_x][ly] == '.' && in[goal_x][ry] == '.') { check_and_push(goal_x, ly, d + max(respective_y - ly, (T)0.) / v[x]); check_and_push(goal_x, ry, d + max(ry - respective_y, (T)0.) / (v[0] - v[goal_x])); } if (in[x][y + 1] == '.') { auto it = Free[goal_x].upper_bound(ly); while (it != Free[goal_x].end()) { T diff = *it - respective_y; if (diff / (v[0] - v[goal_x]) >= 1. / (v[0] - v[x])) { break; } check_and_push(goal_x, *it, d + diff / (v[0] - v[goal_x])); ++it; } } if (y > 1 && in[x][y - 1] == '.') { auto it = Free[goal_x].lower_bound(ry); while (it != Free[goal_x].begin()) { --it; T diff = respective_y - *it; if (diff / (v[x] + v[goal_x]) >= 1. / v[x]) { break; } check_and_push(goal_x, *it, d + diff / (v[x] + v[goal_x])); } } if (x < goal_x) { int next_y = *Free[goal_x].upper_bound(ly); if (next_y <= n && !Used[x][goal_x][next_y]) { Q.push({-d - (next_y - respective_y) / (v[x] - v[goal_x]), -(x * 3 + goal_x), y}); } } else if (ry > 1) { int next_y = *--Free[goal_x].lower_bound(ry); if (next_y > 1 && !Used[x][goal_x][next_y]) { Q.push({-d - (respective_y - next_y) / (v[goal_x] - v[x]), -(x * 3 + goal_x), y}); } } } int main() { scanf("%d", &n); scanf("%d %d %d %d", &v[0], &v[1], &v[2], &v[3]); ++n; for (int i = 1; i <= 3; ++i) { scanf("%s", in[i] + 1); in[i][n] = '.'; in[i][1] = '.'; for (int j = 1; j <= n; ++j) { dist[i][j] = INF; if (in[i][j] == '.') { Free[i].insert(j); } else { best_car[i] = j; } } can_finish[i][n] = true; for (int j = n - 1; j >= 1; --j) { if (in[i][j] == '#') { break; } can_finish[i][j] = true; } dist[i][1] = 0.; Q.push({0., i, 1}); } while (!Q.empty()) { auto [d, type_x, y] = Q.top(); Q.pop(); d *= -1; if (type_x > 0) { int x = type_x; // printf("state %d %d with %.10Lf\n", x, y, d); if (can_finish[x][y]) { update_ans(x, y, d); continue; } if (in[x][y + 1] == '.') { check_and_push(x, y + 1, d + 1. / (v[0] - v[x])); } if (in[x][y - 1] == '.') { check_and_push(x, y - 1, d + 1. / v[x]); } for (int goal_x = 1; goal_x <= 3; ++goal_x) { if (abs(x - goal_x) == 1) { first_move(x, y, goal_x); } } } else { type_x *= -1; int x = (type_x - 1) / 3, goal_x = (type_x - 1) % 3 + 1; int next_y = round(y + d * (v[x] - v[goal_x])); // printf("state %d %d -> %d %d with %.10Lf\n", x, y, goal_x, next_y, d); if (next_y <= 1) { continue; } if (next_y >= n) { update_ans(goal_x, next_y, d); continue; } if (Used[x][goal_x][next_y]) { continue; } Used[x][goal_x][next_y] = true; check_and_push(goal_x, next_y, d); if (x < goal_x) { int new_y = *Free[goal_x].upper_bound(next_y); Q.push({-d - (new_y - next_y) / (T)(v[x] - v[goal_x]), -(x * 3 + goal_x), y}); } else { int new_y = *--Free[goal_x].find(next_y); Q.push({-d - (next_y - new_y) / (T)(v[goal_x] - v[x]), -(x * 3 + goal_x), y}); } } } printf("%.15Lf\n", ans); return 0; }
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | #include <bits/stdc++.h> using namespace std; typedef long double T; #define st first #define nd second #define PII pair <int, int> const int N = 200'007; const T INF = 1'000'007; const T epsilon = 1e-15; int n, v[4]; char in[4][N]; T ans = INF; int best_car[4]; bool can_finish[4][N]; T dist[4][N]; bool Used[4][4][N]; set <int> Free[4]; priority_queue < tuple <T, int, int> > Q; void update_ans(int x, T y, T d) { T cand_ans = d; for (int p = 1; p <= 3; ++p) { if (p == x || best_car[p] == 0) { continue; } T diff = v[p] * d + best_car[p] + 1 - v[x] * d - y; if (diff < 0) { continue; } cand_ans = max(cand_ans, d + diff / (v[0] - v[p])); } // printf("%d %.10Lf %.10Lf -> %.10Lf\n", x, y, d, cand_ans); ans = min(ans, cand_ans); } void check_and_push(int x, int y, T d) { if (dist[x][y] > d) { // printf("pushes %d %d with %.10Lf\n", x, y, d); dist[x][y] = d; Q.push({-d, x, y}); } } void first_move(int x, int y, int goal_x) { const T &d = dist[x][y]; T respective_y = y + d * (v[x] - v[goal_x]); int ly = floor(respective_y + epsilon); int ry = ceil(respective_y - epsilon); if (ly >= n) { update_ans(goal_x, respective_y, d); return; } if (ly < 1) { return; } if (in[goal_x][ly] == '.' && in[goal_x][ry] == '.') { check_and_push(goal_x, ly, d + max(respective_y - ly, (T)0.) / v[x]); check_and_push(goal_x, ry, d + max(ry - respective_y, (T)0.) / (v[0] - v[goal_x])); } if (in[x][y + 1] == '.') { auto it = Free[goal_x].upper_bound(ly); while (it != Free[goal_x].end()) { T diff = *it - respective_y; if (diff / (v[0] - v[goal_x]) >= 1. / (v[0] - v[x])) { break; } check_and_push(goal_x, *it, d + diff / (v[0] - v[goal_x])); ++it; } } if (y > 1 && in[x][y - 1] == '.') { auto it = Free[goal_x].lower_bound(ry); while (it != Free[goal_x].begin()) { --it; T diff = respective_y - *it; if (diff / (v[x] + v[goal_x]) >= 1. / v[x]) { break; } check_and_push(goal_x, *it, d + diff / (v[x] + v[goal_x])); } } if (x < goal_x) { int next_y = *Free[goal_x].upper_bound(ly); if (next_y <= n && !Used[x][goal_x][next_y]) { Q.push({-d - (next_y - respective_y) / (v[x] - v[goal_x]), -(x * 3 + goal_x), y}); } } else if (ry > 1) { int next_y = *--Free[goal_x].lower_bound(ry); if (next_y > 1 && !Used[x][goal_x][next_y]) { Q.push({-d - (respective_y - next_y) / (v[goal_x] - v[x]), -(x * 3 + goal_x), y}); } } } int main() { scanf("%d", &n); scanf("%d %d %d %d", &v[0], &v[1], &v[2], &v[3]); ++n; for (int i = 1; i <= 3; ++i) { scanf("%s", in[i] + 1); in[i][n] = '.'; in[i][1] = '.'; for (int j = 1; j <= n; ++j) { dist[i][j] = INF; if (in[i][j] == '.') { Free[i].insert(j); } else { best_car[i] = j; } } can_finish[i][n] = true; for (int j = n - 1; j >= 1; --j) { if (in[i][j] == '#') { break; } can_finish[i][j] = true; } dist[i][1] = 0.; Q.push({0., i, 1}); } while (!Q.empty()) { auto [d, type_x, y] = Q.top(); Q.pop(); d *= -1; if (type_x > 0) { int x = type_x; // printf("state %d %d with %.10Lf\n", x, y, d); if (can_finish[x][y]) { update_ans(x, y, d); continue; } if (in[x][y + 1] == '.') { check_and_push(x, y + 1, d + 1. / (v[0] - v[x])); } if (in[x][y - 1] == '.') { check_and_push(x, y - 1, d + 1. / v[x]); } for (int goal_x = 1; goal_x <= 3; ++goal_x) { if (abs(x - goal_x) == 1) { first_move(x, y, goal_x); } } } else { type_x *= -1; int x = (type_x - 1) / 3, goal_x = (type_x - 1) % 3 + 1; int next_y = round(y + d * (v[x] - v[goal_x])); // printf("state %d %d -> %d %d with %.10Lf\n", x, y, goal_x, next_y, d); if (next_y <= 1) { continue; } if (next_y >= n) { update_ans(goal_x, next_y, d); continue; } if (Used[x][goal_x][next_y]) { continue; } Used[x][goal_x][next_y] = true; check_and_push(goal_x, next_y, d); if (x < goal_x) { int new_y = *Free[goal_x].upper_bound(next_y); Q.push({-d - (new_y - next_y) / (T)(v[x] - v[goal_x]), -(x * 3 + goal_x), y}); } else { int new_y = *--Free[goal_x].find(next_y); Q.push({-d - (next_y - new_y) / (T)(v[goal_x] - v[x]), -(x * 3 + goal_x), y}); } } } printf("%.15Lf\n", ans); return 0; } |