#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const ll MAXN = 5e4 + 10;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
struct querie
{
ll ql, qr, index;
querie(){};
querie(ll _ql, ll _qr, ll _index)
{
ql = _ql;
qr = _qr;
index = _index;
}
};
ll k, n, m, q;
vector < pair < ll, ll > > g[MAXN];
void read()
{
cin >> k >> n >> m >> q;
ll from, to, cost;
for (ll i = 1; i <= m; ++ i)
{
cin >> from >> to >> cost;
g[from].push_back(make_pair(to, cost));
}
}
void queries()
{
ll ql, qr;
for (ll i = 1; i <= q; ++ i)
{
cin >> ql >> qr;
int st_bucket = ql/k, fi_bucket = qr/k;
vector < ll > ans(k, 1e17);
ans[ql % k] = 0;
for (int i = st_bucket+1; i <= fi_bucket; ++ i) /// go throught the buckets
{
vector < ll > curr(k, 1e17);
for (int st = 0; st < k; ++ st) ///
{
for (auto &[nb, cost]: g[(i-1) * k + st])
{
curr[nb%k] = min(curr[nb%k], cost + ans[st]);
}
}
ans = curr;
}
if(ans[qr%k] == 1e17)cout << -1 << endl;
else cout << ans[qr%k] << endl;
}
}
int main()
{
speed();
read();
queries();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3065 ms |
3164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3036 ms |
3928 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
0 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
64 ms |
3164 KB |
Output is correct |
11 |
Correct |
54 ms |
5468 KB |
Output is correct |
12 |
Correct |
50 ms |
7516 KB |
Output is correct |
13 |
Correct |
58 ms |
8188 KB |
Output is correct |
14 |
Correct |
49 ms |
6480 KB |
Output is correct |
15 |
Correct |
32 ms |
4700 KB |
Output is correct |
16 |
Correct |
25 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
0 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Correct |
2 ms |
1628 KB |
Output is correct |
9 |
Correct |
1 ms |
1628 KB |
Output is correct |
10 |
Correct |
64 ms |
3164 KB |
Output is correct |
11 |
Correct |
54 ms |
5468 KB |
Output is correct |
12 |
Correct |
50 ms |
7516 KB |
Output is correct |
13 |
Correct |
58 ms |
8188 KB |
Output is correct |
14 |
Correct |
49 ms |
6480 KB |
Output is correct |
15 |
Correct |
32 ms |
4700 KB |
Output is correct |
16 |
Correct |
25 ms |
4696 KB |
Output is correct |
17 |
Correct |
1016 ms |
5964 KB |
Output is correct |
18 |
Correct |
1 ms |
1628 KB |
Output is correct |
19 |
Correct |
1 ms |
1628 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
0 ms |
1628 KB |
Output is correct |
22 |
Correct |
0 ms |
1408 KB |
Output is correct |
23 |
Correct |
22 ms |
1712 KB |
Output is correct |
24 |
Correct |
12 ms |
1628 KB |
Output is correct |
25 |
Correct |
15 ms |
1840 KB |
Output is correct |
26 |
Correct |
12 ms |
1628 KB |
Output is correct |
27 |
Correct |
1546 ms |
3928 KB |
Output is correct |
28 |
Correct |
814 ms |
7768 KB |
Output is correct |
29 |
Correct |
856 ms |
8528 KB |
Output is correct |
30 |
Correct |
823 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3065 ms |
3164 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |