#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second struct TimeType { ll num, den; TimeType(int x=0) { num = x; den = 1; } TimeType(int x, int y) { num = x; den = y; if (den == 0) { num = 1e8; den = 1; } } inline void norm() { ll g = __gcd(num, den); num /= g; den /= g; if (den < 0) { num = -num; den = -den; } } TimeType operator+ (const TimeType &t) { TimeType res; ll newden = den / __gcd(den, t.den) * t.den; ll num1 = num * (newden / den), num2 = t.num * (newden / t.den); res.num = num1 + num2; res.den = newden; res.norm(); return res; } TimeType operator- (const TimeType &t) { TimeType res; ll newden = den / __gcd(den, t.den) * t.den; ll num1 = num * (newden / den), num2 = t.num * (newden / t.den); res.num = num1 - num2; res.den = newden; res.norm(); return res; } TimeType operator* (const int &ti) { TimeType t(ti); TimeType res; res.num = num * t.num; res.den = den * t.den; res.norm(); return res; } TimeType operator* (const TimeType &t) { TimeType res; res.num = num * t.num; res.den = den * t.den; res.norm(); return res; } TimeType operator/ (const TimeType &t) { TimeType res; res.num = num * t.den; res.den = den * t.num; res.norm(); return res; } bool operator!= (const TimeType t) const { return num != t.num || den != t.den; } bool operator< (const TimeType t) const { return num*t.den < t.num*den; } bool operator<= (const TimeType t) const { return num*t.den <= t.num*den; } int floor() { return num / den; } double real() { return 1.0 * num / den; } }; ostream& operator << (ostream &out, TimeType in) { //out << in.num << "/" << in.den; out << in.real() << " (" << in.num << "/" << in.den << ")"; return out; } const int N = 200200; const TimeType inf(1,0); int L, v0, v[4]; char stmp[N]; int occ[4][N]; int dst[4][N]; int endlane[4]; TimeType ans_free; TimeType besttime[4][N]; struct State { TimeType t; int lane, pos; State(TimeType t_=0.0, int lane_=-1, int pos_=0) { t=t_; lane=lane_; pos=pos_; } bool operator< (const State &s) const { if (t != s.t) return s.t < t; if (lane != s.lane) return lane < s.lane; return pos < s.pos; } bool bad() { return lane == -1; } }; priority_queue<State> Q; void add(State s) { if (besttime[s.lane][s.pos] <= s.t) { //cerr << "not adding lane " << s.lane << ", position " << s.pos << " at time " << s.t << " : there was time = " << besttime[s.lane][s.pos] << "available\n"; return; } besttime[s.lane][s.pos] = s.t; Q.push(s); //cerr << "adding lane " << s.lane << ", position " << s.pos << " at time " << s.t << "\n"; } void add_free_car(State s) { //cerr << "free car at lane " << s.lane << ", position " << s.pos << " at time " << s.t << "\n"; TimeType ans = 0; FORI(i,3) if (i!=s.lane) { TimeType td = (TimeType(endlane[i] - s.pos) + s.t * (v[i]-v[s.lane])) / (v0-v[i]); ans = max(ans, s.t+max(td,TimeType(0))); } ans_free = min(ans_free, ans); } State get() { while (!Q.empty()) { State s = Q.top(); Q.pop(); if (besttime[s.lane][s.pos] < s.t) continue; return s; } return State(0.0, -1, 0.0); } void chg_lane(State s, int nlane) { if (nlane < 1 || nlane > 3) return; /// speeds: i=here, j=other int vi = v[s.lane], vj = v[nlane]; /// space ahead int dd = dst[s.lane][s.pos] - 1; if (s.pos == L) dd = 1000100; /// check if I can go full speed and then to xj FORI(xj,L) if (!occ[nlane][xj]) { TimeType td = (TimeType(xj - s.pos) + s.t * (vj-vi)) / (v0-vj); TimeType tend = TimeType(1) / (v0-vi); //cerr << "trying " << nlane << ", " << xj << " with dt = " << td << " (td*(v0-vi) = " << (td * (v0-vi)); //cerr << " | inequalities : " << (TimeType(0) <= td) << " " << (td * (v0-vi) <= TimeType(dd)) << "\n"; if (TimeType(0) <= td && td * (v0-vi) <= TimeType(dd) && td <= tend) { add(State(s.t + td, nlane, xj)); } } /// check if I can go to lane j and then full speed to xj { TimeType posj = TimeType(s.pos) + s.t * (vi-vj); int xj = posj.floor() + 1; //cerr << "trying just jump (pos = " << xj << "\n"; if (xj >= 2 && xj <= L && !occ[nlane][xj-1] && !occ[nlane][xj]) { TimeType td = (TimeType(xj - s.pos) + s.t * (vj-vi)) / (v0-vj); add(State(s.t + td, nlane, xj)); } /// or if I have a solution if (xj > L) { TimeType td = (TimeType(xj - s.pos) + s.t * (vj-vi)) / (v0-vj); add_free_car(State(s.t+td, nlane, xj)); } } /// check if I can go to the end, wait and then to go xj FORI(xj,L) if (!occ[nlane][xj]) { TimeType td = (TimeType(xj - s.pos - dd) + s.t * (vj-vi)) / (vi-vj); TimeType tend = TimeType(dd) / (v0-vi); //cerr << "trying " << xj << " with dt = " << td << ", tend = " << tend << "\n"; if (tend <= td) { add(State(s.t + td, nlane, xj)); } } } void run() { FORI(i,3) FORI(j,L) besttime[i][j] = inf; add(State(0.0, 1, 1)); while (true) { State s = get(); //cerr << "going from lane " << s.lane << ", position " << s.pos << " at time " << s.t << "\n"; if (s.bad()) return; if (!occ[s.lane][s.pos+1]) { //cerr << s.t << " + " << (TimeType(1) / (v0-v[s.lane])) << " = " << (s.t + TimeType(1) / (v0-v[s.lane])) << "\n"; add(State(s.t + TimeType(1) / (v0-v[s.lane]), s.lane, s.pos+1)); } if (endlane[s.lane] <= s.pos) { add_free_car(s); } chg_lane(s, s.lane-1); chg_lane(s, s.lane+1); } } int main() { scanf("%d%d%d%d%d", &L, &v0, &v[1], &v[2], &v[3]); FORI(i,3) { scanf(" %s", stmp); FOR(j,L) occ[i][j+1] = stmp[j] == '#'; } L++; occ[3][1] = 0; FORI(i,3) dst[i][L+1] = 1000100; FORI(i,3) REPD(j,L,1) { if (occ[i][j]) dst[i][j] = 0; else dst[i][j] = dst[i][j+1] + 1; } FORI(i,3) { REPD(j,L,1) if (occ[i][j]) { endlane[i] = j+1; break; } } ans_free = inf; run(); TimeType res = 0.0; FORI(i,3) { TimeType cur = inf; REPD(j,L,1) { if (!occ[i][j]) { cur = min(cur, besttime[i][j]); } else { break; } } res = max(res, cur); //FORI(j,L) cerr << besttime[i][j] << " "; //cerr << "\n"; } res = min(res, ans_free); printf("%.12f\n", res.real()); 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second struct TimeType { ll num, den; TimeType(int x=0) { num = x; den = 1; } TimeType(int x, int y) { num = x; den = y; if (den == 0) { num = 1e8; den = 1; } } inline void norm() { ll g = __gcd(num, den); num /= g; den /= g; if (den < 0) { num = -num; den = -den; } } TimeType operator+ (const TimeType &t) { TimeType res; ll newden = den / __gcd(den, t.den) * t.den; ll num1 = num * (newden / den), num2 = t.num * (newden / t.den); res.num = num1 + num2; res.den = newden; res.norm(); return res; } TimeType operator- (const TimeType &t) { TimeType res; ll newden = den / __gcd(den, t.den) * t.den; ll num1 = num * (newden / den), num2 = t.num * (newden / t.den); res.num = num1 - num2; res.den = newden; res.norm(); return res; } TimeType operator* (const int &ti) { TimeType t(ti); TimeType res; res.num = num * t.num; res.den = den * t.den; res.norm(); return res; } TimeType operator* (const TimeType &t) { TimeType res; res.num = num * t.num; res.den = den * t.den; res.norm(); return res; } TimeType operator/ (const TimeType &t) { TimeType res; res.num = num * t.den; res.den = den * t.num; res.norm(); return res; } bool operator!= (const TimeType t) const { return num != t.num || den != t.den; } bool operator< (const TimeType t) const { return num*t.den < t.num*den; } bool operator<= (const TimeType t) const { return num*t.den <= t.num*den; } int floor() { return num / den; } double real() { return 1.0 * num / den; } }; ostream& operator << (ostream &out, TimeType in) { //out << in.num << "/" << in.den; out << in.real() << " (" << in.num << "/" << in.den << ")"; return out; } const int N = 200200; const TimeType inf(1,0); int L, v0, v[4]; char stmp[N]; int occ[4][N]; int dst[4][N]; int endlane[4]; TimeType ans_free; TimeType besttime[4][N]; struct State { TimeType t; int lane, pos; State(TimeType t_=0.0, int lane_=-1, int pos_=0) { t=t_; lane=lane_; pos=pos_; } bool operator< (const State &s) const { if (t != s.t) return s.t < t; if (lane != s.lane) return lane < s.lane; return pos < s.pos; } bool bad() { return lane == -1; } }; priority_queue<State> Q; void add(State s) { if (besttime[s.lane][s.pos] <= s.t) { //cerr << "not adding lane " << s.lane << ", position " << s.pos << " at time " << s.t << " : there was time = " << besttime[s.lane][s.pos] << "available\n"; return; } besttime[s.lane][s.pos] = s.t; Q.push(s); //cerr << "adding lane " << s.lane << ", position " << s.pos << " at time " << s.t << "\n"; } void add_free_car(State s) { //cerr << "free car at lane " << s.lane << ", position " << s.pos << " at time " << s.t << "\n"; TimeType ans = 0; FORI(i,3) if (i!=s.lane) { TimeType td = (TimeType(endlane[i] - s.pos) + s.t * (v[i]-v[s.lane])) / (v0-v[i]); ans = max(ans, s.t+max(td,TimeType(0))); } ans_free = min(ans_free, ans); } State get() { while (!Q.empty()) { State s = Q.top(); Q.pop(); if (besttime[s.lane][s.pos] < s.t) continue; return s; } return State(0.0, -1, 0.0); } void chg_lane(State s, int nlane) { if (nlane < 1 || nlane > 3) return; /// speeds: i=here, j=other int vi = v[s.lane], vj = v[nlane]; /// space ahead int dd = dst[s.lane][s.pos] - 1; if (s.pos == L) dd = 1000100; /// check if I can go full speed and then to xj FORI(xj,L) if (!occ[nlane][xj]) { TimeType td = (TimeType(xj - s.pos) + s.t * (vj-vi)) / (v0-vj); TimeType tend = TimeType(1) / (v0-vi); //cerr << "trying " << nlane << ", " << xj << " with dt = " << td << " (td*(v0-vi) = " << (td * (v0-vi)); //cerr << " | inequalities : " << (TimeType(0) <= td) << " " << (td * (v0-vi) <= TimeType(dd)) << "\n"; if (TimeType(0) <= td && td * (v0-vi) <= TimeType(dd) && td <= tend) { add(State(s.t + td, nlane, xj)); } } /// check if I can go to lane j and then full speed to xj { TimeType posj = TimeType(s.pos) + s.t * (vi-vj); int xj = posj.floor() + 1; //cerr << "trying just jump (pos = " << xj << "\n"; if (xj >= 2 && xj <= L && !occ[nlane][xj-1] && !occ[nlane][xj]) { TimeType td = (TimeType(xj - s.pos) + s.t * (vj-vi)) / (v0-vj); add(State(s.t + td, nlane, xj)); } /// or if I have a solution if (xj > L) { TimeType td = (TimeType(xj - s.pos) + s.t * (vj-vi)) / (v0-vj); add_free_car(State(s.t+td, nlane, xj)); } } /// check if I can go to the end, wait and then to go xj FORI(xj,L) if (!occ[nlane][xj]) { TimeType td = (TimeType(xj - s.pos - dd) + s.t * (vj-vi)) / (vi-vj); TimeType tend = TimeType(dd) / (v0-vi); //cerr << "trying " << xj << " with dt = " << td << ", tend = " << tend << "\n"; if (tend <= td) { add(State(s.t + td, nlane, xj)); } } } void run() { FORI(i,3) FORI(j,L) besttime[i][j] = inf; add(State(0.0, 1, 1)); while (true) { State s = get(); //cerr << "going from lane " << s.lane << ", position " << s.pos << " at time " << s.t << "\n"; if (s.bad()) return; if (!occ[s.lane][s.pos+1]) { //cerr << s.t << " + " << (TimeType(1) / (v0-v[s.lane])) << " = " << (s.t + TimeType(1) / (v0-v[s.lane])) << "\n"; add(State(s.t + TimeType(1) / (v0-v[s.lane]), s.lane, s.pos+1)); } if (endlane[s.lane] <= s.pos) { add_free_car(s); } chg_lane(s, s.lane-1); chg_lane(s, s.lane+1); } } int main() { scanf("%d%d%d%d%d", &L, &v0, &v[1], &v[2], &v[3]); FORI(i,3) { scanf(" %s", stmp); FOR(j,L) occ[i][j+1] = stmp[j] == '#'; } L++; occ[3][1] = 0; FORI(i,3) dst[i][L+1] = 1000100; FORI(i,3) REPD(j,L,1) { if (occ[i][j]) dst[i][j] = 0; else dst[i][j] = dst[i][j+1] + 1; } FORI(i,3) { REPD(j,L,1) if (occ[i][j]) { endlane[i] = j+1; break; } } ans_free = inf; run(); TimeType res = 0.0; FORI(i,3) { TimeType cur = inf; REPD(j,L,1) { if (!occ[i][j]) { cur = min(cur, besttime[i][j]); } else { break; } } res = max(res, cur); //FORI(j,L) cerr << besttime[i][j] << " "; //cerr << "\n"; } res = min(res, ans_free); printf("%.12f\n", res.real()); return 0; } |