#include "escape_route.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
#define ll long long
using namespace std;
const ll inf = 100'000'000'000'000'000;
ll d1[90][90 * 90 + 5][90], d2[90][90], from[90];
vector<ll> tp[90];
vector<pll> last[90][90];
int getT(int i, ll t)
{
return upper_bound(tp[i].begin(), tp[i].end(), t, greater<>()) - tp[i].begin() - 1;
}
void update(int N, int r, int t, int u, int v, ll w)
{
int vt = getT(v, d1[r][t][u] + w);
if (vt < 0)
return;
for (int i = 0; i < N; i++)
d1[r][t][i] = min(d1[r][t][u] + w + d1[v][vt][i] - tp[v][vt], d1[r][t][i]);
}
pll calcNextEdge(int N, int r, ll T, int t)
{
ll nt = -1, ni = -1, nj = -1;
for (int i = 0; i < N; i++)
if (!last[r][i].empty())
if (nt < T - (d1[r][t][i] - last[r][i].back().F))
nt = T - (d1[r][t][i] - last[r][i].back().F), ni = i;
if (ni >= 0)
{
nj = last[r][ni].back().S;
last[r][ni].pop_back();
}
return pll(nj, nt);
}
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)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
last[i][A[j]].emplace_back(pll(C[j] - L[j], j));
last[i][B[j]].emplace_back(pll(C[j] - L[j], j));
}
for (int j = 0; j < N; j++)
sort(last[i][j].begin(), last[i][j].end());
}
priority_queue<tuple<ll, int, int>> pq;
for (int i = 0; i < N; i++)
{
tp[i].emplace_back(S - 1);
for (int j = 0; j < N; j++)
d1[i][0][j] = (i == j ? S - 1 : inf << 1);
auto [j, nt] = calcNextEdge(N, i, tp[i].back(), (int)tp[i].size() - 1);
if (nt >= 0)
pq.push(make_tuple(nt, i, j));
}
while (!pq.empty())
{
auto [t, i, j] = pq.top();
pq.pop();
if (t < tp[i].back())
{
tp[i].emplace_back(t);
int tt = (int)tp[i].size() - 1;
for (int k = 0; k < N; k++)
d1[i][tt][k] = d1[i][tt - 1][k] - (tp[i][tt - 1] - tp[i][tt]);
}
update(N, i, (int)tp[i].size() - 1, A[j], B[j], L[j]);
update(N, i, (int)tp[i].size() - 1, B[j], A[j], L[j]);
auto [k, nt] = calcNextEdge(N, i, tp[i].back(), (int)tp[i].size() - 1);
if (nt >= 0)
pq.push(make_tuple(nt, i, k));
}
// for (int i = 0; i < N; i++)
// for (int j = 0; j < tp[i].size(); j++)
// {
// cerr << i << " (time " << tp[i][j] << ") ";
// for (int k = 0; k < N; k++)
// cout << d1[i][j][k] << " \n"[k == N - 1];
// }
vector<pair<int, pll>> E[90];
for (int i = 0; i < M; i++)
{
E[A[i]].emplace_back(make_pair(B[i], pll(C[i], L[i])));
E[B[i]].emplace_back(make_pair(A[i], pll(C[i], L[i])));
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
d2[i][j] = (i == j ? 0 : inf);
priority_queue<pll, vector<pll>, greater<pll>> pq;
pq.push(pll(0, i));
while (!pq.empty())
{
auto [d, u] = pq.top();
pq.pop();
if (d > d2[i][u])
continue;
for (auto [v, p] : E[u])
{
auto [c, l] = p;
ll nd = (d % S <= c - l ? d : (d + S - 1) / S * S) + l;
if (nd < d2[i][v])
{
d2[i][v] = nd;
pq.push(pll(d2[i][v], v));
}
}
}
}
// for (int i = 0; i < N; i++)
// for (int j = 0; j < N; j++)
// cerr << d2[i][j] << " \n"[j == N - 1];
vector<ll> ans(Q);
for (int i = 0; i < Q; i++)
{
int t = upper_bound(tp[U[i]].begin(), tp[U[i]].end(), T[i], greater<>()) - tp[U[i]].begin() - 1;
ans[i] = inf;
for (int j = 0; j < N; j++)
if (d1[U[i]][t][j] < inf)
{
if (j == V[i])
ans[i] = min(ans[i], d1[U[i]][t][j] - tp[U[i]][t]);
else
ans[i] = min(ans[i], S + d2[j][V[i]] - T[i]);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
65252 KB |
Output is correct |
2 |
Correct |
28 ms |
65248 KB |
Output is correct |
3 |
Correct |
73 ms |
65216 KB |
Output is correct |
4 |
Correct |
25 ms |
65228 KB |
Output is correct |
5 |
Correct |
29 ms |
65236 KB |
Output is correct |
6 |
Correct |
27 ms |
65228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
822 ms |
152160 KB |
Output is correct |
2 |
Correct |
753 ms |
173420 KB |
Output is correct |
3 |
Correct |
752 ms |
161616 KB |
Output is correct |
4 |
Correct |
851 ms |
177608 KB |
Output is correct |
5 |
Correct |
848 ms |
179052 KB |
Output is correct |
6 |
Correct |
25 ms |
65236 KB |
Output is correct |
7 |
Correct |
758 ms |
161288 KB |
Output is correct |
8 |
Correct |
417 ms |
147428 KB |
Output is correct |
9 |
Correct |
742 ms |
153592 KB |
Output is correct |
10 |
Correct |
848 ms |
176992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
65252 KB |
Output is correct |
2 |
Correct |
28 ms |
65248 KB |
Output is correct |
3 |
Correct |
73 ms |
65216 KB |
Output is correct |
4 |
Correct |
25 ms |
65228 KB |
Output is correct |
5 |
Correct |
29 ms |
65236 KB |
Output is correct |
6 |
Correct |
27 ms |
65228 KB |
Output is correct |
7 |
Correct |
822 ms |
152160 KB |
Output is correct |
8 |
Correct |
753 ms |
173420 KB |
Output is correct |
9 |
Correct |
752 ms |
161616 KB |
Output is correct |
10 |
Correct |
851 ms |
177608 KB |
Output is correct |
11 |
Correct |
848 ms |
179052 KB |
Output is correct |
12 |
Correct |
25 ms |
65236 KB |
Output is correct |
13 |
Correct |
758 ms |
161288 KB |
Output is correct |
14 |
Correct |
417 ms |
147428 KB |
Output is correct |
15 |
Correct |
742 ms |
153592 KB |
Output is correct |
16 |
Correct |
848 ms |
176992 KB |
Output is correct |
17 |
Correct |
1134 ms |
160624 KB |
Output is correct |
18 |
Correct |
1138 ms |
156384 KB |
Output is correct |
19 |
Correct |
902 ms |
174768 KB |
Output is correct |
20 |
Correct |
1041 ms |
160332 KB |
Output is correct |
21 |
Correct |
1165 ms |
177868 KB |
Output is correct |
22 |
Correct |
1135 ms |
176240 KB |
Output is correct |
23 |
Correct |
1081 ms |
158540 KB |
Output is correct |
24 |
Correct |
491 ms |
142988 KB |
Output is correct |
25 |
Correct |
1057 ms |
147476 KB |
Output is correct |
26 |
Correct |
1142 ms |
172120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
65252 KB |
Output is correct |
2 |
Correct |
28 ms |
65248 KB |
Output is correct |
3 |
Correct |
73 ms |
65216 KB |
Output is correct |
4 |
Correct |
25 ms |
65228 KB |
Output is correct |
5 |
Correct |
29 ms |
65236 KB |
Output is correct |
6 |
Correct |
27 ms |
65228 KB |
Output is correct |
7 |
Correct |
822 ms |
152160 KB |
Output is correct |
8 |
Correct |
753 ms |
173420 KB |
Output is correct |
9 |
Correct |
752 ms |
161616 KB |
Output is correct |
10 |
Correct |
851 ms |
177608 KB |
Output is correct |
11 |
Correct |
848 ms |
179052 KB |
Output is correct |
12 |
Correct |
25 ms |
65236 KB |
Output is correct |
13 |
Correct |
758 ms |
161288 KB |
Output is correct |
14 |
Correct |
417 ms |
147428 KB |
Output is correct |
15 |
Correct |
742 ms |
153592 KB |
Output is correct |
16 |
Correct |
848 ms |
176992 KB |
Output is correct |
17 |
Correct |
1134 ms |
160624 KB |
Output is correct |
18 |
Correct |
1138 ms |
156384 KB |
Output is correct |
19 |
Correct |
902 ms |
174768 KB |
Output is correct |
20 |
Correct |
1041 ms |
160332 KB |
Output is correct |
21 |
Correct |
1165 ms |
177868 KB |
Output is correct |
22 |
Correct |
1135 ms |
176240 KB |
Output is correct |
23 |
Correct |
1081 ms |
158540 KB |
Output is correct |
24 |
Correct |
491 ms |
142988 KB |
Output is correct |
25 |
Correct |
1057 ms |
147476 KB |
Output is correct |
26 |
Correct |
1142 ms |
172120 KB |
Output is correct |
27 |
Correct |
1646 ms |
261064 KB |
Output is correct |
28 |
Correct |
1721 ms |
279504 KB |
Output is correct |
29 |
Correct |
1219 ms |
319820 KB |
Output is correct |
30 |
Correct |
1578 ms |
293812 KB |
Output is correct |
31 |
Correct |
1681 ms |
327852 KB |
Output is correct |
32 |
Correct |
1673 ms |
329016 KB |
Output is correct |
33 |
Correct |
540 ms |
198100 KB |
Output is correct |
34 |
Correct |
1637 ms |
292980 KB |
Output is correct |
35 |
Correct |
1682 ms |
329800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
65252 KB |
Output is correct |
2 |
Correct |
28 ms |
65248 KB |
Output is correct |
3 |
Correct |
73 ms |
65216 KB |
Output is correct |
4 |
Correct |
25 ms |
65228 KB |
Output is correct |
5 |
Correct |
29 ms |
65236 KB |
Output is correct |
6 |
Correct |
27 ms |
65228 KB |
Output is correct |
7 |
Correct |
822 ms |
152160 KB |
Output is correct |
8 |
Correct |
753 ms |
173420 KB |
Output is correct |
9 |
Correct |
752 ms |
161616 KB |
Output is correct |
10 |
Correct |
851 ms |
177608 KB |
Output is correct |
11 |
Correct |
848 ms |
179052 KB |
Output is correct |
12 |
Correct |
25 ms |
65236 KB |
Output is correct |
13 |
Correct |
758 ms |
161288 KB |
Output is correct |
14 |
Correct |
417 ms |
147428 KB |
Output is correct |
15 |
Correct |
742 ms |
153592 KB |
Output is correct |
16 |
Correct |
848 ms |
176992 KB |
Output is correct |
17 |
Correct |
1134 ms |
160624 KB |
Output is correct |
18 |
Correct |
1138 ms |
156384 KB |
Output is correct |
19 |
Correct |
902 ms |
174768 KB |
Output is correct |
20 |
Correct |
1041 ms |
160332 KB |
Output is correct |
21 |
Correct |
1165 ms |
177868 KB |
Output is correct |
22 |
Correct |
1135 ms |
176240 KB |
Output is correct |
23 |
Correct |
1081 ms |
158540 KB |
Output is correct |
24 |
Correct |
491 ms |
142988 KB |
Output is correct |
25 |
Correct |
1057 ms |
147476 KB |
Output is correct |
26 |
Correct |
1142 ms |
172120 KB |
Output is correct |
27 |
Correct |
1646 ms |
261064 KB |
Output is correct |
28 |
Correct |
1721 ms |
279504 KB |
Output is correct |
29 |
Correct |
1219 ms |
319820 KB |
Output is correct |
30 |
Correct |
1578 ms |
293812 KB |
Output is correct |
31 |
Correct |
1681 ms |
327852 KB |
Output is correct |
32 |
Correct |
1673 ms |
329016 KB |
Output is correct |
33 |
Correct |
540 ms |
198100 KB |
Output is correct |
34 |
Correct |
1637 ms |
292980 KB |
Output is correct |
35 |
Correct |
1682 ms |
329800 KB |
Output is correct |
36 |
Correct |
3158 ms |
667076 KB |
Output is correct |
37 |
Correct |
2861 ms |
574072 KB |
Output is correct |
38 |
Correct |
3081 ms |
643324 KB |
Output is correct |
39 |
Correct |
2982 ms |
682180 KB |
Output is correct |
40 |
Correct |
2997 ms |
682312 KB |
Output is correct |
41 |
Correct |
581 ms |
199028 KB |
Output is correct |
42 |
Correct |
3070 ms |
655228 KB |
Output is correct |
43 |
Correct |
2924 ms |
567444 KB |
Output is correct |