// bk jak to wejdzie na dodatnią liczbę punktów #include <iostream> #include <string> #include <vector> #include <queue> using namespace std; using pii = pair<int, int>; const double DBL_EPS = 1e-9; const double DBL_MAX = 1e400; int v0, v[3]; vector<pii> segments[3]; vector<pii> parseLane(string lane) { vector<pii> ret; for (int i = 0; i < lane.size(); i++) { if (lane[i] == '.') { int start = i; while (i < lane.size() && lane[i] == '.') { i++; } ret.push_back({ start, i - 1 }); } } if (lane[lane.size() - 1] == '#') { ret.push_back({ lane.size(), 123456789 }); } else { ret.back().second = 123456789; } return ret; } pair<double, double> traverse( double time, int fromID, int fromSegment, double offset, int toID, int toSegment ) { double pos = time * v[fromID] + offset + segments[fromID][fromSegment].first; double currentEnd = time * v[fromID] + segments[fromID][fromSegment].second; double toStart = time * v[toID] + segments[toID][toSegment].first; double toEnd = time * v[toID] + segments[toID][toSegment].second; // too late :c if (pos > toEnd + DBL_EPS) { if (fromID < toID) return { time, 0.0 }; return { DBL_MAX, 0.0 }; } // can switch immediately if (pos > toStart - DBL_EPS) { return { time, pos - toStart }; } // must wait // time to move to the front of current lane double dist0 = currentEnd - pos; double time0 = dist0 / (v0 - v[fromID]); // time till we reach the target segment at max speed double dist1 = toStart - pos; double time1 = dist1 / (v0 - v[toID]); // can switch before reaching the front of current lane if (time1 < time0 + DBL_EPS) { return { time + time1, 0.0 }; } // reached the front of current lane if (fromID > toID) { // will not be able to switch lane :c return { DBL_MAX, 0.0 }; } time += time0; pos += v0 * time0; toStart += v[toID] * time0; double dist2 = toStart - pos; double time2 = dist2 / (v[fromID] - v[toID]); return { time + time2, 0.0 }; } struct posinfo { double time; double offset; int lane; int segment; int entry; friend bool operator<(const posinfo &l, const posinfo &r) { return l.time > r.time; } }; int main() { int len; cin >> len >> v0 >> v[0] >> v[1] >> v[2]; string lane[3]; cin >> lane[0] >> lane[1] >> lane[2]; lane[2][0] = '.'; for (int i = 0; i < 3; i++) { segments[i] = parseLane(lane[i]); } vector<double> minTime[3]; vector<double> minTimeOffset[3]; vector<double> minTimeWeight[3]; for (int i = 0; i < 3; i++) { minTime[i] = vector<double>(segments[i].size()); minTimeOffset[i] = vector<double>(segments[i].size()); minTimeWeight[i] = vector<double>(segments[i].size()); for (int j = 1; j < segments[i].size(); j++) { minTimeWeight[i][j] = DBL_MAX; } } // xD priority_queue<posinfo> Q; Q.push({ 0.0, 0.0, 0, 0, 0 }); Q.push({ 0.0, 0.0, 1, 0, 0 }); Q.push({ 0.0, 0.0, 2, 0, 0 }); while (!Q.empty()) { double time = Q.top().time; double offset = Q.top().offset; int lane = Q.top().lane; int segment = Q.top().segment; int entry = Q.top().entry; Q.pop(); double distToEnd1 = segments[lane][segment].second - segments[lane][segment].first - offset; double weight1 = time + distToEnd1 / (v0 - v[lane]); if (weight1 < minTimeWeight[lane][segment] - DBL_EPS) { continue; } //printf("%.10lf %.10lf %d %d %.10lf\n", time, offset, lane, segment, weight1); /*if (abs(minTime[lane][segment] - time) > DBL_EPS || abs(minTimeOffset[lane][segment] - offset) > DBL_EPS) { continue; }*/ //if (minTime[lane][segment] < time - DBL_EPS) { // puts("skip"); // continue; //} //puts(""); //printf("%.10lf %d %d\n", time, lane, segment); for (int laneTo = max(0, lane - 1); laneTo <= min(2, lane + 1); laneTo++) { if (laneTo == lane) continue; //if (minTime[laneTo].size() == segments[laneTo].size()) { // already reached the end of this lane // continue; //} for (int segmentTo = 0; segmentTo < segments[laneTo].size(); segmentTo++) { //printf("%d %d -> %d %d (%d %d)\n", lane, segment, laneTo, segmentTo, minTime[lane].size(), minTimeOffset[lane].size()); auto [t, off] = traverse( time, lane, segment, minTimeOffset[lane][segment], laneTo, segmentTo ); double distToEnd = segments[laneTo][segmentTo].second - segments[laneTo][segmentTo].first - off; double weight = t + distToEnd / (v0 - v[laneTo]); if (t - time > DBL_EPS || entry == laneTo - lane) { if (weight < minTimeWeight[laneTo][segmentTo]) { Q.push({ t, off, laneTo, segmentTo, laneTo - lane }); minTime[laneTo][segmentTo] = t; minTimeOffset[laneTo][segmentTo] = off; minTimeWeight[laneTo][segmentTo] = weight; } } } } } double bestTime = DBL_MAX; for (int i = 0; i < 3; i++) { // calculate time until passing all cars on other lanes //printf("%d %d\n", minTime[i].size(), segments[i].size()); double time = minTime[i].back(); double pos = time * v[i] + minTimeOffset[i].back() + segments[i].back().first; double remainingTime = 0.0; for (int j = 0; j < 3; j++) { double end = time * v[j] + segments[j].back().first; double distance = max(0.0, end - pos); remainingTime = max(remainingTime, distance / (v0 - v[j])); } //printf("%.10lf %.10lf\n", time, remainingTime); bestTime = min(bestTime, time + remainingTime); } printf("%.15lf\n", bestTime); }
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 | // bk jak to wejdzie na dodatnią liczbę punktów #include <iostream> #include <string> #include <vector> #include <queue> using namespace std; using pii = pair<int, int>; const double DBL_EPS = 1e-9; const double DBL_MAX = 1e400; int v0, v[3]; vector<pii> segments[3]; vector<pii> parseLane(string lane) { vector<pii> ret; for (int i = 0; i < lane.size(); i++) { if (lane[i] == '.') { int start = i; while (i < lane.size() && lane[i] == '.') { i++; } ret.push_back({ start, i - 1 }); } } if (lane[lane.size() - 1] == '#') { ret.push_back({ lane.size(), 123456789 }); } else { ret.back().second = 123456789; } return ret; } pair<double, double> traverse( double time, int fromID, int fromSegment, double offset, int toID, int toSegment ) { double pos = time * v[fromID] + offset + segments[fromID][fromSegment].first; double currentEnd = time * v[fromID] + segments[fromID][fromSegment].second; double toStart = time * v[toID] + segments[toID][toSegment].first; double toEnd = time * v[toID] + segments[toID][toSegment].second; // too late :c if (pos > toEnd + DBL_EPS) { if (fromID < toID) return { time, 0.0 }; return { DBL_MAX, 0.0 }; } // can switch immediately if (pos > toStart - DBL_EPS) { return { time, pos - toStart }; } // must wait // time to move to the front of current lane double dist0 = currentEnd - pos; double time0 = dist0 / (v0 - v[fromID]); // time till we reach the target segment at max speed double dist1 = toStart - pos; double time1 = dist1 / (v0 - v[toID]); // can switch before reaching the front of current lane if (time1 < time0 + DBL_EPS) { return { time + time1, 0.0 }; } // reached the front of current lane if (fromID > toID) { // will not be able to switch lane :c return { DBL_MAX, 0.0 }; } time += time0; pos += v0 * time0; toStart += v[toID] * time0; double dist2 = toStart - pos; double time2 = dist2 / (v[fromID] - v[toID]); return { time + time2, 0.0 }; } struct posinfo { double time; double offset; int lane; int segment; int entry; friend bool operator<(const posinfo &l, const posinfo &r) { return l.time > r.time; } }; int main() { int len; cin >> len >> v0 >> v[0] >> v[1] >> v[2]; string lane[3]; cin >> lane[0] >> lane[1] >> lane[2]; lane[2][0] = '.'; for (int i = 0; i < 3; i++) { segments[i] = parseLane(lane[i]); } vector<double> minTime[3]; vector<double> minTimeOffset[3]; vector<double> minTimeWeight[3]; for (int i = 0; i < 3; i++) { minTime[i] = vector<double>(segments[i].size()); minTimeOffset[i] = vector<double>(segments[i].size()); minTimeWeight[i] = vector<double>(segments[i].size()); for (int j = 1; j < segments[i].size(); j++) { minTimeWeight[i][j] = DBL_MAX; } } // xD priority_queue<posinfo> Q; Q.push({ 0.0, 0.0, 0, 0, 0 }); Q.push({ 0.0, 0.0, 1, 0, 0 }); Q.push({ 0.0, 0.0, 2, 0, 0 }); while (!Q.empty()) { double time = Q.top().time; double offset = Q.top().offset; int lane = Q.top().lane; int segment = Q.top().segment; int entry = Q.top().entry; Q.pop(); double distToEnd1 = segments[lane][segment].second - segments[lane][segment].first - offset; double weight1 = time + distToEnd1 / (v0 - v[lane]); if (weight1 < minTimeWeight[lane][segment] - DBL_EPS) { continue; } //printf("%.10lf %.10lf %d %d %.10lf\n", time, offset, lane, segment, weight1); /*if (abs(minTime[lane][segment] - time) > DBL_EPS || abs(minTimeOffset[lane][segment] - offset) > DBL_EPS) { continue; }*/ //if (minTime[lane][segment] < time - DBL_EPS) { // puts("skip"); // continue; //} //puts(""); //printf("%.10lf %d %d\n", time, lane, segment); for (int laneTo = max(0, lane - 1); laneTo <= min(2, lane + 1); laneTo++) { if (laneTo == lane) continue; //if (minTime[laneTo].size() == segments[laneTo].size()) { // already reached the end of this lane // continue; //} for (int segmentTo = 0; segmentTo < segments[laneTo].size(); segmentTo++) { //printf("%d %d -> %d %d (%d %d)\n", lane, segment, laneTo, segmentTo, minTime[lane].size(), minTimeOffset[lane].size()); auto [t, off] = traverse( time, lane, segment, minTimeOffset[lane][segment], laneTo, segmentTo ); double distToEnd = segments[laneTo][segmentTo].second - segments[laneTo][segmentTo].first - off; double weight = t + distToEnd / (v0 - v[laneTo]); if (t - time > DBL_EPS || entry == laneTo - lane) { if (weight < minTimeWeight[laneTo][segmentTo]) { Q.push({ t, off, laneTo, segmentTo, laneTo - lane }); minTime[laneTo][segmentTo] = t; minTimeOffset[laneTo][segmentTo] = off; minTimeWeight[laneTo][segmentTo] = weight; } } } } } double bestTime = DBL_MAX; for (int i = 0; i < 3; i++) { // calculate time until passing all cars on other lanes //printf("%d %d\n", minTime[i].size(), segments[i].size()); double time = minTime[i].back(); double pos = time * v[i] + minTimeOffset[i].back() + segments[i].back().first; double remainingTime = 0.0; for (int j = 0; j < 3; j++) { double end = time * v[j] + segments[j].back().first; double distance = max(0.0, end - pos); remainingTime = max(remainingTime, distance / (v0 - v[j])); } //printf("%.10lf %.10lf\n", time, remainingTime); bestTime = min(bestTime, time + remainingTime); } printf("%.15lf\n", bestTime); } |