#include "escape_route.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 91;
ll dp[N][N * N][N], moment[N][N];
int siz[N];
vector<ll> calculate_necessary_time(int n, int m, ll S, int q, vector<int> A, vector<int> B, vector<ll> L, vector<ll> C,
vector<int> U, vector<int> V, vector<ll> T) {
constexpr ll inf = 3e18;
vector<ll> answer(q, inf);
vector<vector<int>> queries(n);
for (int i = 0; i < q; ++i) {
queries[U[i]].push_back(i);
}
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < m; ++i) {
adj[A[i]].emplace_back(B[i], i);
adj[B[i]].emplace_back(A[i], i);
}
auto dijkstra = [&](int source, ll s, bool rev = false) -> vector<ll> {
vector<ll> dist(n, rev ? -inf : inf);
dist[source] = s;
vector<bool> used(n);
for (int _ = 0; _ < n; ++_) {
int v = -1;
for (int i = 0; i < n; ++i) {
if (!used[i] && (v == -1 || rev ^ (dist[i] < dist[v]))) {
v = i;
}
}
used[v] = true;
for (auto [to, i] : adj[v]) {
ll d;
if (rev) {
d = min(dist[v], C[i]) - L[i];
} else {
d = L[i] + (dist[v] % S <= C[i] - L[i] ? dist[v] : (dist[v] / S + 1) * S);
}
if (rev ^ (dist[to] > d)) {
dist[to] = d;
}
}
}
for (int i = 0; i < n; ++i) {
dist[i] = rev ? s - dist[i] : dist[i] - s;
}
return dist;
};
vector from(n, vector<ll>(n)), dto(n, vector<ll>(n));
for (int i = 0; i < n; ++i) {
from[i] = dijkstra(i, 0);
}
for (int i = 0; i < n; ++i) {
dto[i] = dijkstra(i, S - 1, true);
}
for (int i = 0; i < n; ++i) {
for (int j : queries[i]) {
for (int mid = 0; mid < n; ++mid) {
ll dist = S - 1 - dto[mid][i] < T[j] ? inf : S - T[j] + from[mid][V[j]];
answer[j] = min(answer[j], dist);
}
}
}
vector<array<ll, 4>> events; //time, idx (if query then ~), x
vector till(n, vector<ll>(m, -inf));
for (int i = 0; i < m; ++i) {
int a = A[i], b = B[i];
ll t = C[i] - L[i];
auto da = dijkstra(a, t, true);
auto db = dijkstra(b, t, true);
for (int x = 0; x < n; ++x) {
da[x] = t >= da[x] ? t - da[x] : -inf;
db[x] = t >= db[x] ? t - db[x] : -inf;
till[x][i] = max(da[x], db[x]);
}
}
for (int x = 0; x < n; ++x) {
for (int j : queries[x]) {
events.push_back({T[j], -inf, ~j, x});
}
for (int i = 0; i < m; ++i) {
if (till[x][i] >= 0) {
events.push_back({till[x][i], -(C[i] - L[i]), i, x});
}
}
}
sort(events.rbegin(), events.rend());
for (int i = 0; i < n; ++i) {
siz[i] = 1;
fill_n(dp[i][0], n, inf);
dp[i][0][i] = 0;
moment[i][0] = inf;
}
auto distQuery = [&](int x, ll t) {
int r = upper_bound(moment[x], moment[x] + siz[x], t, greater<>()) - moment[x];
assert(r != 0 && (r == siz[x] || moment[x][r] < t) && moment[x][r - 1] >= t);
return r - 1;
};
for (int ii = 0, jj = 0; ii < events.size(); ) {
while (jj < events.size() && events[jj][0] == events[ii][0]) {
jj += 1;
}
sort(events.begin() + ii, events.begin() + jj, [&](const auto &a, const auto &b) {
int xa = a[3];
int xb = b[3];
int ia = a[2];
int ib = b[2];
if (ia < 0 || ib < 0) {
return ia < 0 ? false : true;
}
ll dista = min(dp[xa][siz[xa] - 1][A[ia]], dp[xa][siz[xa] - 1][B[ia]]);
ll distb = min(dp[xb][siz[xb] - 1][A[ib]], dp[xb][siz[xb] - 1][B[ib]]);
return dista < distb;
});
for (; ii < jj; ++ii) {
auto [ti, useless, j, x] = events[ii];
if (j < 0) {
j = ~j;
ll d = dp[x][siz[x] - 1][V[j]];
if (T[j] + d < S) {
answer[j] = min(answer[j], d);
}
continue;
}
memcpy(dp[x][siz[x]], dp[x][siz[x] - 1], sizeof(dp[x][siz[x]]));
moment[x][siz[x]] = ti;
int a = A[j], b = B[j];
if (dp[x][siz[x] - 1][a] > dp[x][siz[x] - 1][b]) {
swap(a, b);
}
ll distB = dp[x][siz[x] - 1][a] + L[j];
// cout << x << " " << j << endl;
assert(ti + distB <= C[j]);
if (distB < dp[x][siz[x] - 1][b]) {
int r = distQuery(b, distB);
for (int to = 0; to < n; ++to) {
dp[x][siz[x]][to] = min(dp[x][siz[x]][to], dp[b][r][to] + distB);
}
}
siz[x] += 1;
}
}
return answer;
}
Compilation message
escape_route.cpp: In function 'std::vector<long long int> calculate_necessary_time(int, int, ll, int, std::vector<int>, std::vector<int>, std::vector<long long int>, std::vector<long long int>, std::vector<int>, std::vector<int>, std::vector<long long int>)':
escape_route.cpp:128:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for (int ii = 0, jj = 0; ii < events.size(); ) {
| ~~~^~~~~~~~~~~~~~~
escape_route.cpp:129:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | while (jj < events.size() && events[jj][0] == events[ii][0]) {
| ~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
64972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1324 ms |
231504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
64972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
64972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
64972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |