#include <bits/stdc++.h> using namespace std; int L, v0; int v[3]; double EPSILON = 1e-10; vector<pair<double, double>> lanes[3]; void readLane(vector<pair<double, double>> &lane, bool removeFirst= false){ string laneDescription; cin >> laneDescription; laneDescription += '.'; int begin = -1; int end = -1; if(removeFirst) laneDescription[0] = '.'; for(int i = 0; i < laneDescription.size(); i++){ if(laneDescription[i] == '.') { if(begin == -1){ continue; } lane.emplace_back(begin, end); begin = -1; end = -1; } else { if(begin == -1) { begin = i; end = i+1; } else { end++; } } } } struct positionDetails{ double time; int line; int behindUs; double position; }; int getBeforeOurPosition(int line, double time, double position) { pair<double, double> low_val = {position - time*v[line]+1, position - time*v[line]}; auto it = lower_bound(lanes[line].begin(), lanes[line].end(), low_val, [](pair<double, double> p1, pair<double, double> p2) -> bool {return p1.first - p2.first < EPSILON; }); if(it == lanes[line].begin()) return -1; it = prev(it, 1); return it - lanes[line].begin(); } int getEndBeforeOurPosition(int line, double time, double position) { pair<double, double> low_val = {position, position - time*v[line]}; auto it = upper_bound(lanes[line].begin(), lanes[line].end(), low_val, [](pair<double, double> p1, pair<double, double> p2) -> bool {return p1.second - p2.second < -EPSILON; }); if(it == lanes[line].begin()) return -1; it = prev(it, 1); return it - lanes[line].begin(); } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; double cutTo12Number(double number){ auto tmp = (long long)number; number -= (double)tmp; auto tmp2 = (double)((long long)(number*1e12))/(1e12); return (double)tmp + tmp2; } unordered_map<pair<double, double>, bool, hash_pair> um[3]; bool inHashMap(double time, double position, int line){ if(um[line].count({cutTo12Number(position), cutTo12Number(time)}) > 0) return true; um[line][{cutTo12Number(position), cutTo12Number(time)}] = true; return false; } double findFastestWay() { auto cmp = [] (positionDetails &p1, positionDetails &p2) { return p1.time > p2.time; }; priority_queue<positionDetails, vector<positionDetails>, decltype(cmp)> q(cmp); q.push({0, 2, 0, 0}); bool firstIteration = true; while (!q.empty()) { positionDetails pos = q.top(); q.pop(); if((lanes[0].empty() or ((pos.time*v[0] + lanes[0].back().second) - pos.position)<EPSILON) and (lanes[1].empty() or ((pos.time*v[1] + lanes[1].back().second) - pos.position)<EPSILON) and (lanes[2].empty() or ((pos.time*v[2] + lanes[2].back().second) - pos.position)<EPSILON)) { return pos.time; } if(lanes[pos.line].size() <= pos.behindUs) { double timeMax = 0; if(!lanes[0].empty()) { double distance = (pos.time*v[0] + lanes[0].back().second) - pos.position; if(distance <= 0) timeMax = max(timeMax, pos.time); else { double tmpTime = distance/(v0 - v[0]); timeMax = max(timeMax, pos.time + tmpTime); } } if(!lanes[1].empty()) { double distance = (pos.time*v[1] + lanes[1].back().second) - pos.position; if(distance <= 0) timeMax = max(timeMax, pos.time); else { double tmpTime = distance/(v0 - v[1]); timeMax = max(timeMax, pos.time + tmpTime); } } if(!lanes[2].empty()) { double distance = (pos.time*v[2] + lanes[2].back().second) - pos.position; if(distance <= 0) timeMax = max(timeMax, pos.time); else { double tmpTime = distance/(v0 - v[2]); timeMax = max(timeMax, pos.time + tmpTime); } } if(!inHashMap(timeMax, pos.position + (timeMax-pos.time)*v0, pos.line)) q.push({timeMax, pos.line, pos.behindUs, pos.position + (timeMax-pos.time)*v0}); continue; } double nextCar = lanes[pos.line][pos.behindUs].first; if(pos.line > 0){ // idziemy w gore - szybsza double distance = nextCar + pos.time*v[pos.line] - 1; double time = (distance-pos.position)/(v0 - v[pos.line]); double nextPosition = pos.position + time*v0; double nextTime = pos.time + time; int idEndBeforeOurEnd = getEndBeforeOurPosition(pos.line - 1, nextTime, nextPosition); for(int i = idEndBeforeOurEnd; i <= idEndBeforeOurEnd; i++) { if(i == -1){ if(pos.line == 1 or firstIteration) { firstIteration = false; if(!inHashMap(pos.time, pos.position, pos.line-1)) q.push({pos.time, pos.line-1, 0, pos.position}); } continue; } double carPosition = lanes[pos.line-1][i].second + pos.time*v[pos.line-1]; if(carPosition - pos.position < EPSILON) { //CHANGED if(pos.line == 1 or firstIteration) { if(!inHashMap(pos.time, pos.position, pos.line-1)) q.push({pos.time, pos.line-1, i+1, pos.position}); firstIteration = false; } } else { double tmpDistance = carPosition - pos.position - 1; double tmpTime = tmpDistance/(v0 - v[pos.line-1]); double tmpNextPosition = pos.position + tmpTime*v0; double tmpNextTime = pos.time + tmpTime; if(!inHashMap(tmpNextTime, tmpNextPosition, pos.line-1)) q.push({tmpNextTime, pos.line-1, i+1, tmpNextPosition}); } } } if(pos.line < 2) { // idziemy w dol - wolniejsza double distance = nextCar + pos.time*v[pos.line] - 1; double time = (distance-pos.position)/(v0 - v[pos.line]); double nextPosition = pos.position + time*v0; double nextTime = pos.time + time; // int idEndBeforeOurEnd = getEndBeforeOurPosition(pos.line + 1, nextTime, nextPosition); int idBeginBeforeOurPosition = getBeforeOurPosition(pos.line + 1, pos.time, pos.position); for(int i = (int)lanes[pos.line + 1].size() - 1;i >= idBeginBeforeOurPosition; i--) { // for(int i = idEndBeforeOurEnd;i >= idBeginBeforeOurPosition; i--) { if(i >= lanes[pos.line+1].size()) continue; if(i == -1){ if(pos.line == 1){ if(!inHashMap(pos.time, pos.position, pos.line+1)) q.push({pos.time, pos.line+1, 0, pos.position}); } continue; } double carPosition = lanes[pos.line+1][i].second + pos.time*v[pos.line+1]; if(carPosition <= pos.position) { if(pos.line == 1){ if(!inHashMap(pos.time, pos.position, pos.line+1)) q.push({pos.time, pos.line+1, i+1, pos.position}); break; } } else { double tmpDistance = carPosition - pos.position; double tmpTime = tmpDistance/(v0 - v[pos.line+1]); if(nextCar + (pos.time + tmpTime)*v[pos.line] >= lanes[pos.line+1][i].second + (pos.time + tmpTime)*v[pos.line+1] + 1) { double tmpNextPosition = pos.position + tmpTime*v0; double tmpNextTime = pos.time + tmpTime; if(!inHashMap(tmpNextTime, tmpNextPosition, pos.line+1)) q.push({tmpNextTime, pos.line+1, i+1, tmpNextPosition}); } else { double tmpTime1 = (lanes[pos.line+1][i].second - nextCar+1)/(v[pos.line] - v[pos.line+1]); double tmpNextPosition = nextCar + v[pos.line]*tmpTime1 - 1; if(!inHashMap(tmpTime1, tmpNextPosition, pos.line+1)) q.push({tmpTime1, pos.line+1, i+1, tmpNextPosition}); } } // break; } } } return 0.0; } int main() { std::ios_base::sync_with_stdio(false); cin >> L >> v0 >> v[0] >> v[1] >> v[2]; readLane(lanes[0]); readLane(lanes[1]); readLane(lanes[2], true); // printf("%.*f\n", 10, findFastestWay()); printf("%.13g\n", findFastestWay()); 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 | #include <bits/stdc++.h> using namespace std; int L, v0; int v[3]; double EPSILON = 1e-10; vector<pair<double, double>> lanes[3]; void readLane(vector<pair<double, double>> &lane, bool removeFirst= false){ string laneDescription; cin >> laneDescription; laneDescription += '.'; int begin = -1; int end = -1; if(removeFirst) laneDescription[0] = '.'; for(int i = 0; i < laneDescription.size(); i++){ if(laneDescription[i] == '.') { if(begin == -1){ continue; } lane.emplace_back(begin, end); begin = -1; end = -1; } else { if(begin == -1) { begin = i; end = i+1; } else { end++; } } } } struct positionDetails{ double time; int line; int behindUs; double position; }; int getBeforeOurPosition(int line, double time, double position) { pair<double, double> low_val = {position - time*v[line]+1, position - time*v[line]}; auto it = lower_bound(lanes[line].begin(), lanes[line].end(), low_val, [](pair<double, double> p1, pair<double, double> p2) -> bool {return p1.first - p2.first < EPSILON; }); if(it == lanes[line].begin()) return -1; it = prev(it, 1); return it - lanes[line].begin(); } int getEndBeforeOurPosition(int line, double time, double position) { pair<double, double> low_val = {position, position - time*v[line]}; auto it = upper_bound(lanes[line].begin(), lanes[line].end(), low_val, [](pair<double, double> p1, pair<double, double> p2) -> bool {return p1.second - p2.second < -EPSILON; }); if(it == lanes[line].begin()) return -1; it = prev(it, 1); return it - lanes[line].begin(); } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; double cutTo12Number(double number){ auto tmp = (long long)number; number -= (double)tmp; auto tmp2 = (double)((long long)(number*1e12))/(1e12); return (double)tmp + tmp2; } unordered_map<pair<double, double>, bool, hash_pair> um[3]; bool inHashMap(double time, double position, int line){ if(um[line].count({cutTo12Number(position), cutTo12Number(time)}) > 0) return true; um[line][{cutTo12Number(position), cutTo12Number(time)}] = true; return false; } double findFastestWay() { auto cmp = [] (positionDetails &p1, positionDetails &p2) { return p1.time > p2.time; }; priority_queue<positionDetails, vector<positionDetails>, decltype(cmp)> q(cmp); q.push({0, 2, 0, 0}); bool firstIteration = true; while (!q.empty()) { positionDetails pos = q.top(); q.pop(); if((lanes[0].empty() or ((pos.time*v[0] + lanes[0].back().second) - pos.position)<EPSILON) and (lanes[1].empty() or ((pos.time*v[1] + lanes[1].back().second) - pos.position)<EPSILON) and (lanes[2].empty() or ((pos.time*v[2] + lanes[2].back().second) - pos.position)<EPSILON)) { return pos.time; } if(lanes[pos.line].size() <= pos.behindUs) { double timeMax = 0; if(!lanes[0].empty()) { double distance = (pos.time*v[0] + lanes[0].back().second) - pos.position; if(distance <= 0) timeMax = max(timeMax, pos.time); else { double tmpTime = distance/(v0 - v[0]); timeMax = max(timeMax, pos.time + tmpTime); } } if(!lanes[1].empty()) { double distance = (pos.time*v[1] + lanes[1].back().second) - pos.position; if(distance <= 0) timeMax = max(timeMax, pos.time); else { double tmpTime = distance/(v0 - v[1]); timeMax = max(timeMax, pos.time + tmpTime); } } if(!lanes[2].empty()) { double distance = (pos.time*v[2] + lanes[2].back().second) - pos.position; if(distance <= 0) timeMax = max(timeMax, pos.time); else { double tmpTime = distance/(v0 - v[2]); timeMax = max(timeMax, pos.time + tmpTime); } } if(!inHashMap(timeMax, pos.position + (timeMax-pos.time)*v0, pos.line)) q.push({timeMax, pos.line, pos.behindUs, pos.position + (timeMax-pos.time)*v0}); continue; } double nextCar = lanes[pos.line][pos.behindUs].first; if(pos.line > 0){ // idziemy w gore - szybsza double distance = nextCar + pos.time*v[pos.line] - 1; double time = (distance-pos.position)/(v0 - v[pos.line]); double nextPosition = pos.position + time*v0; double nextTime = pos.time + time; int idEndBeforeOurEnd = getEndBeforeOurPosition(pos.line - 1, nextTime, nextPosition); for(int i = idEndBeforeOurEnd; i <= idEndBeforeOurEnd; i++) { if(i == -1){ if(pos.line == 1 or firstIteration) { firstIteration = false; if(!inHashMap(pos.time, pos.position, pos.line-1)) q.push({pos.time, pos.line-1, 0, pos.position}); } continue; } double carPosition = lanes[pos.line-1][i].second + pos.time*v[pos.line-1]; if(carPosition - pos.position < EPSILON) { //CHANGED if(pos.line == 1 or firstIteration) { if(!inHashMap(pos.time, pos.position, pos.line-1)) q.push({pos.time, pos.line-1, i+1, pos.position}); firstIteration = false; } } else { double tmpDistance = carPosition - pos.position - 1; double tmpTime = tmpDistance/(v0 - v[pos.line-1]); double tmpNextPosition = pos.position + tmpTime*v0; double tmpNextTime = pos.time + tmpTime; if(!inHashMap(tmpNextTime, tmpNextPosition, pos.line-1)) q.push({tmpNextTime, pos.line-1, i+1, tmpNextPosition}); } } } if(pos.line < 2) { // idziemy w dol - wolniejsza double distance = nextCar + pos.time*v[pos.line] - 1; double time = (distance-pos.position)/(v0 - v[pos.line]); double nextPosition = pos.position + time*v0; double nextTime = pos.time + time; // int idEndBeforeOurEnd = getEndBeforeOurPosition(pos.line + 1, nextTime, nextPosition); int idBeginBeforeOurPosition = getBeforeOurPosition(pos.line + 1, pos.time, pos.position); for(int i = (int)lanes[pos.line + 1].size() - 1;i >= idBeginBeforeOurPosition; i--) { // for(int i = idEndBeforeOurEnd;i >= idBeginBeforeOurPosition; i--) { if(i >= lanes[pos.line+1].size()) continue; if(i == -1){ if(pos.line == 1){ if(!inHashMap(pos.time, pos.position, pos.line+1)) q.push({pos.time, pos.line+1, 0, pos.position}); } continue; } double carPosition = lanes[pos.line+1][i].second + pos.time*v[pos.line+1]; if(carPosition <= pos.position) { if(pos.line == 1){ if(!inHashMap(pos.time, pos.position, pos.line+1)) q.push({pos.time, pos.line+1, i+1, pos.position}); break; } } else { double tmpDistance = carPosition - pos.position; double tmpTime = tmpDistance/(v0 - v[pos.line+1]); if(nextCar + (pos.time + tmpTime)*v[pos.line] >= lanes[pos.line+1][i].second + (pos.time + tmpTime)*v[pos.line+1] + 1) { double tmpNextPosition = pos.position + tmpTime*v0; double tmpNextTime = pos.time + tmpTime; if(!inHashMap(tmpNextTime, tmpNextPosition, pos.line+1)) q.push({tmpNextTime, pos.line+1, i+1, tmpNextPosition}); } else { double tmpTime1 = (lanes[pos.line+1][i].second - nextCar+1)/(v[pos.line] - v[pos.line+1]); double tmpNextPosition = nextCar + v[pos.line]*tmpTime1 - 1; if(!inHashMap(tmpTime1, tmpNextPosition, pos.line+1)) q.push({tmpTime1, pos.line+1, i+1, tmpNextPosition}); } } // break; } } } return 0.0; } int main() { std::ios_base::sync_with_stdio(false); cin >> L >> v0 >> v[0] >> v[1] >> v[2]; readLane(lanes[0]); readLane(lanes[1]); readLane(lanes[2], true); // printf("%.*f\n", 10, findFastestWay()); printf("%.13g\n", findFastestWay()); return 0; } |