#include <bits/stdc++.h> using namespace std; using I = long long; inline I gcd(I a, I b) { return (b ? gcd(b, a % b): a); } struct Rational { I p, q; Rational() : p(0), q(1) {} Rational(I a, I b) : p(a), q(b) { assert(q != 0); if (q < 0) { p = -p; q = -q; } I g = gcd(a, b); p /= g; q /= g; } void print() { cout << "<" << p << "/" << q << ">"; } bool operator==(const Rational &rhs) const { return p == rhs.p && q == rhs.q; } bool operator<(const Rational &rhs) const { return 1LL * p * rhs.q < 1LL * rhs.p * q; } Rational operator+(const Rational &rhs) const { return Rational{p * rhs.q + rhs.p * q, q * rhs.q}; } Rational operator-(const Rational &rhs) const { return Rational{p * rhs.q - rhs.p * q, q * rhs.q}; } Rational operator/(const Rational &rhs) const { return Rational{p * rhs.q, q * rhs.p}; } Rational operator*(const Rational &rhs) const { return Rational{p * rhs.p, q * rhs.q}; } }; int L, lel; Rational v[4]; string s; vector<pair<Rational, Rational>> pos[4]; map<pair<Rational, Rational>, Rational> dist[4]; set<pair<Rational, Rational>> waswas[4]; Rational res = {0, 1}; int32_t main() { ios_base::sync_with_stdio(0); cin >> L; for (int i = 0; i <= 3; i++) { cin >> lel; v[i] = {lel, 1}; } for (int i = 1; i <= 3; i++) { cin >> s; s += ".#"; s[0] = '.'; int l = 0; for (int j = 0; j <= L; j++) { if (s[j] == '.' && s[j + 1] == '#') { pos[i].push_back({{l, 1}, {j + 1, 1}}); } else if (s[j] == '#') l = j + 1; } } dist[3][pos[3][0]] = Rational{0, 1}; dist[1][pos[1][0]] = Rational{0, 1}; dist[2][pos[2][0]] = Rational{0, 1}; priority_queue<pair<pair<Rational, Rational>, pair<int, int>>> pq; pq.push({{Rational{0, 1}, Rational{0, 1}}, {3, 0}}); pq.push({{Rational{0, 1}, Rational{0, 1}}, {1, 0}}); pq.push({{Rational{0, 1}, Rational{0, 1}}, {2, 0}}); while (!pq.empty()) { auto elt = pq.top(); pq.pop(); Rational t = elt.first.first; Rational z = elt.first.second; int pas = elt.second.first; int idx = elt.second.second; if (waswas[pas].count(pos[pas][idx])) continue; waswas[pas].insert(pos[pas][idx]); Rational x_0 = pos[pas][idx].first + v[pas] * t; Rational x_1 = pos[pas][idx].second + v[pas] * t; // x_0.print(); x_1.print(); t.print(); cerr << pas << "\n"; // Może już moge wyprzedzić wszystkich if (idx + 1 == pos[pas].size()) { Rational e_1 = pos[1].back().first; Rational e_2 = pos[2].back().first; Rational e_3 = pos[3].back().first; Rational t_1 = (e_1 + v[0] * t - z) / (v[0] - v[1]); Rational t_2 = (e_2 + v[0] * t - z) / (v[0] - v[2]); Rational t_3 = (e_3 + v[0] * t - z) / (v[0] - v[3]); Rational t_final = max({t_1, t_2, t_3}); res = min(res, t + t_final); continue; } // Przejście na pas wolniejszy if (pas < 3) { int idx = -1; for (auto &i : pos[pas + 1]) { idx++; Rational y_2 = i.first + v[pas + 1] * t; //Rational y_1 = i.second + v[pas + 1] * t; Rational t_1 = (y_2 - z) / (v[0] - v[pas + 1]); Rational t_2 = (x_1 - z - Rational{1, 1}) / (v[0] - v[pas]); Rational t_3 = (y_2 - x_1 + Rational{1, 1}) / (v[pas] - v[pas + 1]); Rational t_final = max({t_1, t_3}); if (t_2 < t_final) continue; if (!dist[pas + 1].count(i) || t_final + t < dist[pas + 1][i]) { dist[pas + 1][i] = t + t_final; pq.push({{t + t_final, z + v[0] * (t_final - t)}, {pas + 1, idx}}); } } } // Przejście na pas szybszy if (pas > 1) { int idx = -1; for (auto &i : pos[pas - 1]) { idx++; Rational y_2 = i.first + v[pas - 1] * t; //Rational y_1 = i.second + v[pas + 1] * t; Rational t_1 = (y_2 - z) / (v[0] - v[pas]); Rational t_2 = (x_1 - z - Rational{1, 1}) / (v[0] - v[pas]); Rational t_3 = (y_2 - x_1 + Rational{1, 1}) / (v[pas - 1] - v[pas]); if (min({t_2, t_3}) < t_1) continue; if (!dist[pas - 1].count(i) || t_1 + t < dist[pas - 1][i]) { dist[pas - 1][i] = t + t_1; pq.push({{t + t_1, z + v[0] * (t_1 - t)}, {pas - 1, idx}}); } } } } cout << fixed << setprecision(10) << ((long double)res.p / res.q) << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; using I = long long; inline I gcd(I a, I b) { return (b ? gcd(b, a % b): a); } struct Rational { I p, q; Rational() : p(0), q(1) {} Rational(I a, I b) : p(a), q(b) { assert(q != 0); if (q < 0) { p = -p; q = -q; } I g = gcd(a, b); p /= g; q /= g; } void print() { cout << "<" << p << "/" << q << ">"; } bool operator==(const Rational &rhs) const { return p == rhs.p && q == rhs.q; } bool operator<(const Rational &rhs) const { return 1LL * p * rhs.q < 1LL * rhs.p * q; } Rational operator+(const Rational &rhs) const { return Rational{p * rhs.q + rhs.p * q, q * rhs.q}; } Rational operator-(const Rational &rhs) const { return Rational{p * rhs.q - rhs.p * q, q * rhs.q}; } Rational operator/(const Rational &rhs) const { return Rational{p * rhs.q, q * rhs.p}; } Rational operator*(const Rational &rhs) const { return Rational{p * rhs.p, q * rhs.q}; } }; int L, lel; Rational v[4]; string s; vector<pair<Rational, Rational>> pos[4]; map<pair<Rational, Rational>, Rational> dist[4]; set<pair<Rational, Rational>> waswas[4]; Rational res = {0, 1}; int32_t main() { ios_base::sync_with_stdio(0); cin >> L; for (int i = 0; i <= 3; i++) { cin >> lel; v[i] = {lel, 1}; } for (int i = 1; i <= 3; i++) { cin >> s; s += ".#"; s[0] = '.'; int l = 0; for (int j = 0; j <= L; j++) { if (s[j] == '.' && s[j + 1] == '#') { pos[i].push_back({{l, 1}, {j + 1, 1}}); } else if (s[j] == '#') l = j + 1; } } dist[3][pos[3][0]] = Rational{0, 1}; dist[1][pos[1][0]] = Rational{0, 1}; dist[2][pos[2][0]] = Rational{0, 1}; priority_queue<pair<pair<Rational, Rational>, pair<int, int>>> pq; pq.push({{Rational{0, 1}, Rational{0, 1}}, {3, 0}}); pq.push({{Rational{0, 1}, Rational{0, 1}}, {1, 0}}); pq.push({{Rational{0, 1}, Rational{0, 1}}, {2, 0}}); while (!pq.empty()) { auto elt = pq.top(); pq.pop(); Rational t = elt.first.first; Rational z = elt.first.second; int pas = elt.second.first; int idx = elt.second.second; if (waswas[pas].count(pos[pas][idx])) continue; waswas[pas].insert(pos[pas][idx]); Rational x_0 = pos[pas][idx].first + v[pas] * t; Rational x_1 = pos[pas][idx].second + v[pas] * t; // x_0.print(); x_1.print(); t.print(); cerr << pas << "\n"; // Może już moge wyprzedzić wszystkich if (idx + 1 == pos[pas].size()) { Rational e_1 = pos[1].back().first; Rational e_2 = pos[2].back().first; Rational e_3 = pos[3].back().first; Rational t_1 = (e_1 + v[0] * t - z) / (v[0] - v[1]); Rational t_2 = (e_2 + v[0] * t - z) / (v[0] - v[2]); Rational t_3 = (e_3 + v[0] * t - z) / (v[0] - v[3]); Rational t_final = max({t_1, t_2, t_3}); res = min(res, t + t_final); continue; } // Przejście na pas wolniejszy if (pas < 3) { int idx = -1; for (auto &i : pos[pas + 1]) { idx++; Rational y_2 = i.first + v[pas + 1] * t; //Rational y_1 = i.second + v[pas + 1] * t; Rational t_1 = (y_2 - z) / (v[0] - v[pas + 1]); Rational t_2 = (x_1 - z - Rational{1, 1}) / (v[0] - v[pas]); Rational t_3 = (y_2 - x_1 + Rational{1, 1}) / (v[pas] - v[pas + 1]); Rational t_final = max({t_1, t_3}); if (t_2 < t_final) continue; if (!dist[pas + 1].count(i) || t_final + t < dist[pas + 1][i]) { dist[pas + 1][i] = t + t_final; pq.push({{t + t_final, z + v[0] * (t_final - t)}, {pas + 1, idx}}); } } } // Przejście na pas szybszy if (pas > 1) { int idx = -1; for (auto &i : pos[pas - 1]) { idx++; Rational y_2 = i.first + v[pas - 1] * t; //Rational y_1 = i.second + v[pas + 1] * t; Rational t_1 = (y_2 - z) / (v[0] - v[pas]); Rational t_2 = (x_1 - z - Rational{1, 1}) / (v[0] - v[pas]); Rational t_3 = (y_2 - x_1 + Rational{1, 1}) / (v[pas - 1] - v[pas]); if (min({t_2, t_3}) < t_1) continue; if (!dist[pas - 1].count(i) || t_1 + t < dist[pas - 1][i]) { dist[pas - 1][i] = t + t_1; pq.push({{t + t_1, z + v[0] * (t_1 - t)}, {pas - 1, idx}}); } } } } cout << fixed << setprecision(10) << ((long double)res.p / res.q) << "\n"; } |