#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define bug(x) cerr << "L" << __LINE__ << " | " << #x << " => " << x << endl
#else
#define bug(x)
#define cerr if (0) cerr
#endif
#define ar array
#define sz(v) int(std::size(v))
#define all(v) std::begin(v), std::end(v)
using i64 = long long;
using vi = vector<int>;
using pi = pair<int, int>;
vector<i64> calculate_necessary_time(int N, int M, i64 S, int Q, vi A, vi B, vector<i64> L, vector<i64> C, vi U, vi V, vector<i64> T) {
const int MAXN = 90;
const i64 INF = 1e18;
static vi adj[MAXN];
for (int i = 0; i < M; i++) adj[A[i]].push_back(i), adj[B[i]].push_back(i);
static i64 dist[MAXN][MAXN], end[MAXN][MAXN];
for (int src = 0; src < N; src++) {
{
fill(dist[src], dist[src] + N, INF);
using X = pair<i64, int>;
priority_queue<X, vector<X>, greater<X>> pq;
auto ins = [&](int i, i64 d) {
if (dist[src][i] > d)
pq.push({dist[src][i] = d, i});
};
ins(src, 0);
while (sz(pq)) {
auto [d, i] = pq.top(); pq.pop();
if (dist[src][i] != d) continue;
for (int h : adj[i]) ins(i ^ A[h] ^ B[h], L[h] + (d % S <= C[h] - L[h] ? d : (d / S + 1) * S));
}
}
{
fill(end[src], end[src] + N, -1);
using X = pair<i64, int>;
priority_queue<X> pq;
auto ins = [&](int i, i64 d) {
if (end[src][i] < d)
pq.push({end[src][i] = d, i});
};
ins(src, S);
while (sz(pq)) {
auto [d, i] = pq.top(); pq.pop();
if (end[src][i] != d) continue;
for (int h : adj[i]) ins(i ^ A[h] ^ B[h], min(C[h], d) - L[h]);
}
}
}
vector<i64> ans(Q);
static vi at_v[MAXN];
for (int i = 0; i < Q; i++) {
i64 x = INF;
for (int j = 0; j < N; j++) if (T[i] <= end[j][U[i]]) x = min(x, S - T[i] + dist[j][V[i]]);
ans[i] = x;
if (dist[U[i]][V[i]] <= S) at_v[U[i]].push_back(i);
}
for (int src = 0; src < N; src++) {
static i64 dist[MAXN];
static bool consider[MAXN];
fill(dist, dist + N, INF);
fill(consider, consider + N, false);
auto upd = [&](int i, i64 d) {
if (dist[i] > d) {
dist[i] = d;
consider[i] = 1;
}
};
i64 START = INF;
priority_queue<pair<i64, pi>> edges;
auto relax = [&]() {
while (1) {
int i = -1;
for (int j = 0; j < N; j++) if (consider[j] && (i == -1 || dist[j] < dist[i])) i = j;
if (i == -1) return;
consider[i] = 0;
for (int h : adj[i]) {
int j = i ^ A[h] ^ B[h];
if (dist[j] <= dist[i] + L[h]) continue;
if (START + dist[i] + L[h] <= C[h]) upd(j, dist[i] + L[h]);
else edges.push({C[h] - dist[i] - L[h], {i, h}});
}
}
};
upd(src, 0);
sort(all(at_v[src]), [&](int i, int j) {
return T[i] > T[j];
});
for (int i : at_v[src]) {
START = T[i];
while (sz(edges) && edges.top().first >= START) {
auto [i, h] = edges.top().second; edges.pop();
int j = i ^ A[h] ^ B[h];
assert(START + dist[i] + L[h] <= C[h]);
upd(j, dist[i] + L[h]);
}
relax();
ans[i] = min(ans[i], dist[V[i]]);
}
}
return ans;
}
#ifdef LOCAL
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, M, Q; i64 S; cin >> N >> M >> S >> Q;
vi A(M), B(M); vector<i64> L(M), C(M);
for (int i = 0; i < M; i++) cin >> A[i] >> B[i] >> L[i] >> C[i];
vi U(Q), V(Q); vector<i64> T(Q);
for (int i = 0; i < Q; i++) cin >> U[i] >> V[i] >> T[i];
auto ans = calculate_necessary_time(N, M, S, Q, A, B, L, C, U, V, T);
assert(sz(ans) == Q);
for (int i = 0; i < Q; i++) cout << ans[i] << '\n';
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
64996 KB |
Output is correct |
2 |
Correct |
9 ms |
65116 KB |
Output is correct |
3 |
Correct |
14 ms |
65116 KB |
Output is correct |
4 |
Correct |
9 ms |
65116 KB |
Output is correct |
5 |
Correct |
10 ms |
65004 KB |
Output is correct |
6 |
Correct |
9 ms |
65116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1135 ms |
120524 KB |
Output is correct |
2 |
Correct |
1178 ms |
133148 KB |
Output is correct |
3 |
Correct |
1131 ms |
124756 KB |
Output is correct |
4 |
Correct |
1179 ms |
135648 KB |
Output is correct |
5 |
Correct |
1214 ms |
136016 KB |
Output is correct |
6 |
Correct |
8 ms |
65116 KB |
Output is correct |
7 |
Correct |
1111 ms |
123724 KB |
Output is correct |
8 |
Correct |
297 ms |
134460 KB |
Output is correct |
9 |
Correct |
1102 ms |
112012 KB |
Output is correct |
10 |
Correct |
1207 ms |
134928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
64996 KB |
Output is correct |
2 |
Correct |
9 ms |
65116 KB |
Output is correct |
3 |
Correct |
14 ms |
65116 KB |
Output is correct |
4 |
Correct |
9 ms |
65116 KB |
Output is correct |
5 |
Correct |
10 ms |
65004 KB |
Output is correct |
6 |
Correct |
9 ms |
65116 KB |
Output is correct |
7 |
Correct |
1135 ms |
120524 KB |
Output is correct |
8 |
Correct |
1178 ms |
133148 KB |
Output is correct |
9 |
Correct |
1131 ms |
124756 KB |
Output is correct |
10 |
Correct |
1179 ms |
135648 KB |
Output is correct |
11 |
Correct |
1214 ms |
136016 KB |
Output is correct |
12 |
Correct |
8 ms |
65116 KB |
Output is correct |
13 |
Correct |
1111 ms |
123724 KB |
Output is correct |
14 |
Correct |
297 ms |
134460 KB |
Output is correct |
15 |
Correct |
1102 ms |
112012 KB |
Output is correct |
16 |
Correct |
1207 ms |
134928 KB |
Output is correct |
17 |
Correct |
1158 ms |
165552 KB |
Output is correct |
18 |
Correct |
1270 ms |
164392 KB |
Output is correct |
19 |
Correct |
1351 ms |
189072 KB |
Output is correct |
20 |
Correct |
1049 ms |
168328 KB |
Output is correct |
21 |
Correct |
1393 ms |
198348 KB |
Output is correct |
22 |
Correct |
1374 ms |
198080 KB |
Output is correct |
23 |
Correct |
1026 ms |
167444 KB |
Output is correct |
24 |
Correct |
394 ms |
197572 KB |
Output is correct |
25 |
Correct |
1133 ms |
157132 KB |
Output is correct |
26 |
Correct |
1393 ms |
197828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
64996 KB |
Output is correct |
2 |
Correct |
9 ms |
65116 KB |
Output is correct |
3 |
Correct |
14 ms |
65116 KB |
Output is correct |
4 |
Correct |
9 ms |
65116 KB |
Output is correct |
5 |
Correct |
10 ms |
65004 KB |
Output is correct |
6 |
Correct |
9 ms |
65116 KB |
Output is correct |
7 |
Correct |
1135 ms |
120524 KB |
Output is correct |
8 |
Correct |
1178 ms |
133148 KB |
Output is correct |
9 |
Correct |
1131 ms |
124756 KB |
Output is correct |
10 |
Correct |
1179 ms |
135648 KB |
Output is correct |
11 |
Correct |
1214 ms |
136016 KB |
Output is correct |
12 |
Correct |
8 ms |
65116 KB |
Output is correct |
13 |
Correct |
1111 ms |
123724 KB |
Output is correct |
14 |
Correct |
297 ms |
134460 KB |
Output is correct |
15 |
Correct |
1102 ms |
112012 KB |
Output is correct |
16 |
Correct |
1207 ms |
134928 KB |
Output is correct |
17 |
Correct |
1158 ms |
165552 KB |
Output is correct |
18 |
Correct |
1270 ms |
164392 KB |
Output is correct |
19 |
Correct |
1351 ms |
189072 KB |
Output is correct |
20 |
Correct |
1049 ms |
168328 KB |
Output is correct |
21 |
Correct |
1393 ms |
198348 KB |
Output is correct |
22 |
Correct |
1374 ms |
198080 KB |
Output is correct |
23 |
Correct |
1026 ms |
167444 KB |
Output is correct |
24 |
Correct |
394 ms |
197572 KB |
Output is correct |
25 |
Correct |
1133 ms |
157132 KB |
Output is correct |
26 |
Correct |
1393 ms |
197828 KB |
Output is correct |
27 |
Correct |
2080 ms |
167300 KB |
Output is correct |
28 |
Correct |
3117 ms |
168616 KB |
Output is correct |
29 |
Correct |
3699 ms |
194992 KB |
Output is correct |
30 |
Correct |
1195 ms |
169288 KB |
Output is correct |
31 |
Correct |
3677 ms |
202128 KB |
Output is correct |
32 |
Correct |
3753 ms |
201480 KB |
Output is correct |
33 |
Correct |
468 ms |
197732 KB |
Output is correct |
34 |
Correct |
2013 ms |
157776 KB |
Output is correct |
35 |
Correct |
3605 ms |
202248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
64996 KB |
Output is correct |
2 |
Correct |
9 ms |
65116 KB |
Output is correct |
3 |
Correct |
14 ms |
65116 KB |
Output is correct |
4 |
Correct |
9 ms |
65116 KB |
Output is correct |
5 |
Correct |
10 ms |
65004 KB |
Output is correct |
6 |
Correct |
9 ms |
65116 KB |
Output is correct |
7 |
Correct |
1135 ms |
120524 KB |
Output is correct |
8 |
Correct |
1178 ms |
133148 KB |
Output is correct |
9 |
Correct |
1131 ms |
124756 KB |
Output is correct |
10 |
Correct |
1179 ms |
135648 KB |
Output is correct |
11 |
Correct |
1214 ms |
136016 KB |
Output is correct |
12 |
Correct |
8 ms |
65116 KB |
Output is correct |
13 |
Correct |
1111 ms |
123724 KB |
Output is correct |
14 |
Correct |
297 ms |
134460 KB |
Output is correct |
15 |
Correct |
1102 ms |
112012 KB |
Output is correct |
16 |
Correct |
1207 ms |
134928 KB |
Output is correct |
17 |
Correct |
1158 ms |
165552 KB |
Output is correct |
18 |
Correct |
1270 ms |
164392 KB |
Output is correct |
19 |
Correct |
1351 ms |
189072 KB |
Output is correct |
20 |
Correct |
1049 ms |
168328 KB |
Output is correct |
21 |
Correct |
1393 ms |
198348 KB |
Output is correct |
22 |
Correct |
1374 ms |
198080 KB |
Output is correct |
23 |
Correct |
1026 ms |
167444 KB |
Output is correct |
24 |
Correct |
394 ms |
197572 KB |
Output is correct |
25 |
Correct |
1133 ms |
157132 KB |
Output is correct |
26 |
Correct |
1393 ms |
197828 KB |
Output is correct |
27 |
Correct |
2080 ms |
167300 KB |
Output is correct |
28 |
Correct |
3117 ms |
168616 KB |
Output is correct |
29 |
Correct |
3699 ms |
194992 KB |
Output is correct |
30 |
Correct |
1195 ms |
169288 KB |
Output is correct |
31 |
Correct |
3677 ms |
202128 KB |
Output is correct |
32 |
Correct |
3753 ms |
201480 KB |
Output is correct |
33 |
Correct |
468 ms |
197732 KB |
Output is correct |
34 |
Correct |
2013 ms |
157776 KB |
Output is correct |
35 |
Correct |
3605 ms |
202248 KB |
Output is correct |
36 |
Correct |
7512 ms |
175720 KB |
Output is correct |
37 |
Execution timed out |
9033 ms |
158096 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |