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
#include <bits/stdc++.h>


using namespace std;


typedef long long val_t;


long double solve(int maxVelocity, vector <int> &velocity, vector <vector<val_t>> &cars) {
    vector <int> allVelocities = velocity;
    allVelocities.insert(allVelocities.begin(), maxVelocity);

    long long denom = 1;
    for (int i = 0; i < 4; i++) for (int j = i + 1; j < 4; j++) {
        denom *= allVelocities[i] - allVelocities[j];
    }

    vector <int> nCars(3);
    vector <vector<pair<val_t,int>>> carsUnblocked(3);
    vector <vector<int>> carBlockStart(3);
    vector <vector<val_t>> dp(3);

    for (int lane = 0; lane < 3; lane++) {
        nCars[lane] = cars[lane].size();
        dp[lane].resize(nCars[lane], -1);
        carBlockStart[lane].resize(nCars[lane]);

        int blockStart = 0;
        for (int carId = 0; carId < nCars[lane]; carId++) {
            carBlockStart[lane][carId] = blockStart;

            if (carId == nCars[lane] - 1 || cars[lane][carId + 1] > cars[lane][carId] + 1) {
                carsUnblocked[lane].push_back({denom * cars[lane][carId], carId});
                blockStart = carId + 1;
            }
        }
    }

    for (int lane = 0; lane < 3; lane++) for (val_t &x : cars[lane]) {
        x *= denom;
    }

    dp[2][0] = 0;

    set <pair<val_t, pair<int,int>>> q;
    q.insert({0, {2, 0}});

    while (!q.empty()) {
        auto [_, s] = *q.begin(); q.erase(q.begin());
        auto [refLane, carId] = s;

        if (carId == nCars[refLane] - 1) {
            continue;
        }

        val_t currTime = dp[refLane][carId];
        val_t pos = cars[refLane][carId] + currTime * velocity[refLane];

        vector <val_t> nextCarId(3);
        vector <val_t> switchDelay(3, 0);

        for (int lane = 0; lane < 3; lane++) {
            val_t threshold = 2 * denom + pos - currTime * velocity[lane];
            nextCarId[lane] = lower_bound(
                cars[lane].begin(), cars[lane].end(), threshold
            ) - cars[lane].begin();

            int blockingCarId = nextCarId[lane] - 1;
            if (blockingCarId >= 0) {
                val_t blockingPos = cars[lane][blockingCarId] + currTime * velocity[lane];
                val_t posDiff = blockingPos - pos;
                if (posDiff > 0) {
                    if (lane < refLane) {
                        int blockStart = carBlockStart[lane][blockingCarId];
                        val_t blockingPart = denom * (blockingCarId - blockStart + 2) - posDiff;

                        switchDelay[lane] = blockingPart / (velocity[lane] - velocity[refLane]);
                        nextCarId[lane] = blockStart;
                    } else {
                        switchDelay[lane] = -1;
                    }
                }
            }
        }

        if (refLane != 1) {
            int flipLane = 2 - refLane;
            if (switchDelay[1] == -1) {
                switchDelay[flipLane] = -1;
            } else if (switchDelay[flipLane] != -1) {
                switchDelay[flipLane] = max(switchDelay[flipLane], switchDelay[1]);
            }
        }

        for (int lane = 0; lane < 3; lane++) if (switchDelay[lane] != -1) {
            val_t currTimeWithDelay = currTime + switchDelay[lane];
            val_t posWithDelay = cars[refLane][carId] + currTimeWithDelay * velocity[refLane];

            for (int otherLane = 0; otherLane < 3; otherLane++) if (abs(lane - otherLane) == 1) {
                val_t threshold = posWithDelay - currTimeWithDelay * velocity[otherLane];

                int firstIdx = lower_bound(
                    carsUnblocked[otherLane].begin(), carsUnblocked[otherLane].end(), make_pair(threshold, -1)
                ) - carsUnblocked[otherLane].begin();

                for (int idx : {firstIdx, firstIdx + 1}) {
                    if (idx == (int) carsUnblocked[otherLane].size()) {
                        break;
                    }

                    int otherCarId = carsUnblocked[otherLane][idx].second;

                    val_t posDiff = cars[otherLane][otherCarId] - threshold, extraTime;
                    if (posDiff <= 0) {
                        extraTime = 0;
                    } else {
                        val_t catchUpTime = posDiff / (maxVelocity - velocity[otherLane]);

                        if (nextCarId[lane] < nCars[lane]) {
                            val_t nextCarPos = cars[lane][nextCarId[lane]] + currTimeWithDelay * velocity[lane];
                            val_t gapSize = nextCarPos - posWithDelay - 2 * denom;

                            val_t maxVelocityTime = gapSize / (maxVelocity - velocity[lane]);
                            if (catchUpTime <= maxVelocityTime) {
                                extraTime = catchUpTime;
                            } else if (lane < otherLane) {
                                posDiff -= maxVelocityTime * (maxVelocity - velocity[otherLane]);
                                extraTime = maxVelocityTime + posDiff / (velocity[lane] - velocity[otherLane]);
                            } else {
                                break;
                            }
                        } else {
                            extraTime = catchUpTime;
                        }
                    }

                    val_t newSol = currTimeWithDelay + extraTime;
                    val_t oldSol = dp[otherLane][otherCarId];

                    if (oldSol == -1 || newSol < oldSol) {
                        if (oldSol != -1) {
                            q.erase({oldSol, {otherLane, otherCarId}});
                        }

                        dp[otherLane][otherCarId] = newSol;
                        q.insert({newSol, {otherLane, otherCarId}});
                    }
                }
            }
        }
    }

    val_t ans = -1;
    for (int lane = 0; lane < 3; lane++) if (nCars[lane] > 0) {
        val_t currTime = dp[lane].back();
        if (currTime == -1) {
            continue;
        }

        val_t pos = cars[lane].back() + currTime * velocity[lane], extraTime = 0;
        for (int otherLane = 0; otherLane < 3; otherLane++) if (nCars[otherLane] > 0) {
            val_t otherPos = cars[otherLane].back() + currTime * velocity[otherLane];
            val_t posDiff = otherPos - pos;

            if (posDiff > 0) {
                extraTime = max(extraTime, posDiff / (maxVelocity - velocity[otherLane]));
            }
        }

        val_t ansHere = currTime + extraTime;
        if (ans == -1 || ansHere < ans) {
            ans = ansHere;
        }
    }

    return ((long double) ans) / denom;
}

int main() {
    ios_base::sync_with_stdio(false);
    cout << setprecision(15) << fixed;

    int len, maxVelocity;
    cin >> len >> maxVelocity;

    vector <int> velocity(3);
    for (int lane = 0; lane < 3; lane++) {
        cin >> velocity[lane];
    }

    vector <vector<val_t>> cars(3);
    for (int lane = 0; lane < 3; lane++) {
        string s;
        cin >> s;

        if (lane < 2) {
            s = "." + s;
        } else {
            s[0] = '.';
            s = "#" + s;
        }

        for (int x = 0; x <= len; x++) if (s[x] == '#') {
            cars[lane].push_back(x);
        }
    }

    cout << solve(maxVelocity, velocity, cars);

    return 0;
}