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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <cstdio>
#include <queue>

const long double eps = 1e-9L;
const int max_length = 300010;

bool equal(long double a, long double b) {
  a -= b;
  return -eps < a && a < eps;
}

bool gt(long double a, long double b) {
  a -= b;
  return a > eps;
}

bool ge(long double a, long double b) {
  a -= b;
  return a > -eps;
}

bool lt(long double a, long double b) {
  a -= b;
  return a < -eps;
}

bool le(long double a, long double b) {
  a -= b;
  return a < eps;
}


struct Hole {
  long long begin;
  long long end;
};

int length, v0;
char buffer[max_length];

int n[3];
int v[3];
Hole ev[3][max_length];

bool visited[3][3][2 * max_length][2];

struct Entry {
  long double t;
  int line;
  int ps_line;
  int ps_edge;
  int ps_side;

  bool operator<(const Entry& e) const {
    return t > e.t;
  }
};

std::priority_queue<Entry> pq;

long double compute_position(long double t, int line, int edge, int side) {
  int e = side == 0 ? ev[line][edge].begin : ev[line][edge].end;
  return t * v[line] + e;
}

void make_entry(int c, long double t, int line, int ps_line, int ps_edge, int ps_side) {
  if (ps_side == 1 && n[ps_line] == ps_edge + 1) {
//    printf("SKIP\n");
    return;
  }
//  printf("CASE: %d %lf %d %d %d %d\n", c, t, line, ps_line, ps_edge, ps_side);
  Entry e;
  e.t = t;
  e.line = line;
  e.ps_line = ps_line;
  e.ps_edge = ps_edge;
  e.ps_side = ps_side;
  pq.push(e);
}

// pierwszy ktory ma koniec nie mniejszy
int find_next_hole(int line, long double t, long double my_position) {
  int left = 0, right = n[line];
  while (left < right) {
    int middle = (left + right) / 2;
    long double begin = ev[line][middle].begin + t * v[line];
    long double end = ev[line][middle].end + t * v[line];
    if (le(begin, my_position) && le(my_position, end)) {
      return middle;
    }
    if (gt(my_position, end)) left = middle + 1;
    else right = middle;
  }
  return left;
}

int main() {
  n[0] = n[1] = n[2] = 0;
  scanf("%d %d %d %d %d", &length, &v0, &v[0], &v[1], &v[2]);

  for (int k = 0; k < 3; ++k) {
    scanf(" %s ", buffer);
    buffer[0] = '.';
    buffer[length] = '.'; 
    buffer[length + 1] = '.';
    buffer[length + 2] = 0;
    int begins = 1;
    for (int i = 1; i <= length + 1 ; ++i) {
      if (buffer[i] == '#' && buffer[i - 1] == '.') {
        ev[k][n[k]].begin = begins;
        ev[k][n[k]].end = i;
        ++n[k];
      }
      if (buffer[i] == '#' && buffer[i + 1] == '.') {
        begins = i + 2;
      }
    }
    // guards.
    ev[k][n[k]].begin = begins;
    ev[k][n[k]].end = 1000000000000000000LL;
    ++n[k];   
  }
  for (int i = 0; i < 2 * max_length; ++i) {
    for (int j = 0; j < 3; ++j) {
      visited[0][j][i][0] = visited[1][j][i][0] = visited[2][j][i][0] = 0;
      visited[0][j][i][1] = visited[1][j][i][1] = visited[2][j][i][1] = 0;
    }
  }

  make_entry(0, 0.0, 2, 2, 0, 0);

  long double ans = 1e9L;
  while (!pq.empty()) {
    long double t0 = pq.top().t;
//    printf("%lf\n", t0);
    int loc_line = pq.top().line;
    int line = pq.top().ps_line;
    int edge = pq.top().ps_edge;
    int side = pq.top().ps_side;
    pq.pop();
    if (visited[loc_line][line][edge][side]) continue;
    if (t0 > ans) continue;
    visited[loc_line][line][edge][side] = 1;
    long double my_position = compute_position(t0, line, edge, side);
//    if (1.0 + t0 * v0 + eps< my_position) printf("BUUUGGG\n");
//    printf("%lf %lf %lf | %d %d %d %d\n", t0, my_position, length + v[0] * t0, loc_line, line, edge, side);
    // this should be always my current one.
    int id = find_next_hole(loc_line, t0, my_position);
    if (ev[loc_line][id].end == 1000000000000000000LL) {
      long double dt = 0.0;
      long double pos_0 = ev[0][n[0] - 1].begin + t0 * v[0];
      if (gt(pos_0,my_position)) {
        long double dd = (pos_0 - my_position) / (v0 - v[0]);
        if (dt < dd) dt = dd;
      }
      long double pos_1 = ev[1][n[1] - 1].begin + t0 * v[1];
      if (gt(pos_1, my_position)) {
        long double dd = (pos_1 - my_position) / (v0 - v[1]);
        if (dt < dd) dt = dd;
      }
      long double pos_2 = ev[2][n[2] - 1].begin + t0 * v[2];
      if (gt(pos_2, my_position)) {
        long double dd = (pos_2 - my_position) / (v0 - v[2]);
        if (dt < dd) dt = dd;
      }
      if (ans > t0 + dt) ans = t0 + dt;
      // we are free to bypass.
//      printf("FREE\n");
      continue;
    }
    if (loc_line == 2) {
      long double end_position = compute_position(t0, loc_line, id, 1);
      long double t1 = (end_position - my_position) / (v0 - v[2]);
      // Case: wskocz na pas 1
      {
        int hole_id = find_next_hole(1, t0, my_position);
        long double hole_begin = compute_position(t0, 1, hole_id, 0);
        if (le(hole_begin, my_position)) {
          if (!visited[1][line][edge][side]) {
            // avoid cycles.
            make_entry(1, t0, 1, line, edge, side);
          }
        }
      }
      // Case: moze zrownaj sie z nastepnym eventem na 1.
      {
        int hole_id = find_next_hole(1, t0, my_position);
        long double hole_end = compute_position(t0, 1, hole_id, 1);
        if (equal(hole_end, my_position)) {
          ++hole_id;
          long double hole_begin = compute_position(t0, 1, hole_id, 0);
          long double time_needed = (hole_begin - my_position) / (v0 - v[1]);
          if (le(time_needed, t1)) {
            // zdarze sie zrownac.
            make_entry(2, t0 + time_needed, 2, 1, hole_id, 0);
          }
        } else {
          long double time_needed = (hole_end - my_position) / (v0 - v[1]);
          if (le(time_needed, t1)) {
            // zdarze sie zrownac.
            make_entry(3, t0 + time_needed, 2, 1, hole_id, 1);
          }
        }        
      }
    } else if (loc_line == 1) {
      long double begin_position = compute_position(t0, loc_line, id, 0);
      long double end_position = compute_position(t0, loc_line, id, 1);
      long double t1 = (end_position - my_position) / (v0 - v[1]);
      long double t2 = (my_position - begin_position) / v[1];

      // Case: podjedz na zderzak
      if (gt(t1, 0.0)) {
         make_entry(4, t0 + t1, 1, 1, id, 1);
      }
      // Case: cofnij na maske
      if (gt(t2, 0.0)) {
         make_entry(5, t0 + t2, 1, 1, id, 0);
      }
      // Case: moze zrownaj sie z nastepnym eventem na 0.
      {
        int hole_id = find_next_hole(0, t0, my_position);
        long double hole_end = compute_position(t0, 0, hole_id, 1);
        if (equal(hole_end, my_position)) {
          ++hole_id;
          long double hole_begin = compute_position(t0, 0, hole_id, 0);
          long double time_needed = (hole_begin - my_position) / (v0 - v[0]);
          if (le(time_needed, t1)) {
            // zdarze sie zrownac.
            make_entry(6, t0 + time_needed, 1, 0, hole_id, 0);
          }
        } else {
          long double time_needed = (hole_end - my_position) / (v0 - v[0]);
          if (le(time_needed, t1)) {
            // zdarze sie zrownac.
            make_entry(7, t0 + time_needed, 1, 0, hole_id, 1);
          }
        }        
      }
      // Case: moze poczekaj na poprzednia dziure?
      {
        int hole_id = find_next_hole(0, t0, my_position);
        long double hole_begin = compute_position(t0, 0, hole_id, 0);
        long double my_speed = 0;
        long double time_got = t2;
        if (equal(my_position, end_position)){
          // jade na masce
          my_speed = v[1];
          time_got = 1e9;
        }
        if (gt(hole_begin, my_position) && hole_id >= 1) {
          long double hole_end = compute_position(t0, 0, hole_id - 1, 1);
          long double time_needed = (my_position - hole_end) / (v[0] - my_speed);
          if (le(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(8, t0 + time_needed, 1, 0, hole_id - 1, 1);
          }
        }
      }
      // Case: wskocz na pas 0
      {
        int hole_id = find_next_hole(0, t0, my_position);
        long double hole_begin = compute_position(t0, 0, hole_id, 0);
        if (le(hole_begin, my_position)) {
          if (!visited[0][line][edge][side]) {
            // avoid cycles.
            make_entry(9, t0, 0, line, edge, side);
          }
        }
      }
      // Case: wskocz na pas 2
      {
        int hole_id = find_next_hole(2, t0, my_position);
        long double hole_begin = compute_position(t0, 2, hole_id, 0);
        if (le(hole_begin, my_position)) {
          if (!visited[2][line][edge][side]) {
            // avoid cycles.
            make_entry(10, t0, 2, line, edge, side);
          }
        }
      }
      // Case: zrownaj sie z nastepnym eventem
      {
        long double my_speed = v0;
        long double time_got = t1;
        if (equal(my_position, end_position)){
          // jade na zderzaku
          my_speed = v[1];
          time_got = 1e9;
        }
        int hole_id = find_next_hole(2, t0, my_position);
        long double hole_end = compute_position(t0, 2, hole_id, 1);
        long double hole_begin = compute_position(t0, 2, hole_id, 0);
        if (equal(hole_end, my_position)) {
          ++hole_id;
          hole_begin = compute_position(t0, 2, hole_id, 0);
          long double time_needed = (hole_begin - my_position) / (my_speed - v[2]);
          if (le(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(11, t0 + time_needed, 1, 2, hole_id, 0);
          }
        } else if (gt(hole_begin, my_position)) {
          long double time_needed = (hole_begin - my_position) / (my_speed - v[2]);
          if (le(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(22, t0 + time_needed, 1, 2, hole_id, 0);
          }
        } else {
          long double time_needed = (hole_end - my_position) / (my_speed - v[2]);
          if (le(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(12, t0 + time_needed, 1, 2, hole_id, 1);
          }
        }        
      }
    } else if (loc_line == 0) {
      long double end_position = compute_position(t0, loc_line, id, 1);
      long double t1 = (end_position - my_position) / (v0 - v[0]);
      // Case: podjedz na zderzak
      if (gt(t1, 0.0)) {
         make_entry(13, t0 + t1, 0, 0, id, 1);
      }
      // Case: zrownaj sie z nastepnym eventem na 1
      {
        long double my_speed = v0;
        long double time_got = t1;
        if (equal(my_position, end_position)){
          // jade na zderzaku
          my_speed = v[0];
          time_got = 1e9;
        }
        int hole_id = find_next_hole(1, t0, my_position);
        long double hole_end = compute_position(t0, 1, hole_id, 1);
        long double hole_begin = compute_position(t0, 1, hole_id, 0);
        if (equal(hole_end, my_position)) {
          ++hole_id;
          hole_begin = compute_position(t0, 1, hole_id, 0);
          long double time_needed = (hole_begin - my_position) / (my_speed - v[1]);
          if (lt(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(14, t0 + time_needed, 0, 1, hole_id, 0);
          }
        } else if (gt(hole_begin, my_position)) {
          long double time_needed = (hole_begin - my_position) / (my_speed - v[1]);
          if (le(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(19, t0 + time_needed, 0, 1, hole_id, 0);
          }
        } else {
          long double time_needed = (hole_end - my_position) / (my_speed - v[1]);
          if (le(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(15, t0 + time_needed, 0, 1, hole_id, 1);
          }
        }        
      }
      // Case: zrownaj sie z nastepnym eventem na 2
      {
        long double my_speed = v0;
        long double time_got = t1;
        if (equal(my_position, end_position)){
          // jade na zderzaku
          my_speed = v[0];
          time_got = 1e9;
        }
        int hole_id = find_next_hole(2, t0, my_position);
        long double hole_end = compute_position(t0, 2, hole_id, 1);
        long double hole_begin = compute_position(t0, 2, hole_id, 0);
        if (equal(hole_end, my_position)) {
          ++hole_id;
          hole_begin = compute_position(t0, 2, hole_id, 0);
          long double time_needed = (hole_begin - my_position) / (my_speed - v[2]);
          if (le(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(17, t0 + time_needed, 0, 2, hole_id, 0);
          }
        } else if (gt(hole_begin, my_position)) {
          long double time_needed = (hole_begin - my_position) / (my_speed - v[2]);
          if (le(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(20, t0 + time_needed, 0, 2, hole_id, 0);
          }
        } else {
          long double time_needed = (hole_end - my_position) / (my_speed - v[2]);
          if (le(time_needed, time_got)) {
            // zdarze sie zrownac.
            make_entry(18, t0 + time_needed, 0, 2, hole_id, 1);
          }
        }        
      }
      // Case: wskocz na pas 1.
      {
        int hole_id = find_next_hole(1, t0, my_position);
        long double hole_begin = compute_position(t0, 1, hole_id, 0);
        if (le(hole_begin, my_position)) {
          if (!visited[1][line][edge][side]) {
            // avoid cycles.
            make_entry(16, t0, 1, line, edge, side);
          }
        }
      }
    }
  }
  printf("%.19Lf\n", ans);
  return 0;
}