/* IN THE NAME OF GOD */
/* |\/| /-\ |\| | |\/| /-\ */
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define endl '\n'
#define pb push_back
#define F first
#define S second
#define mr make_pair
#define pii pair<int, int>
const int N = 5e4 + 1;
int inf = 5e8 + 1;
vector<pii> g[N];
int dis[N], qq[2][10001], ans[10001], mx[N], qqq[10001];
int Q[N][10001];
int pt[N];
int bl[N];
int main(){
ios_base:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
int k, n, m, q;
cin >> k >> n >> m >> q;
int u, v, w;
for(int i = 1; i <= m; i++){
cin >> u >> v >> w;
g[u].pb(mr(v, w));
}
int p = 0;
for(int i = 1; i <= q; i++){
cin >> qq[0][i] >> qq[1][i];
if(qq[0][i] > qq[1][i]){
ans[i] = -1;
continue;
}
if(qq[0][i] == qq[1][i])
continue;
if(qq[0][i] / k == qq[1][i] / k){
ans[i] = -1;
continue;
}
Q[qq[0][i]][pt[qq[0][i]]] = i;
pt[qq[0][i]]++;
mx[qq[0][i]] = max(mx[qq[0][i]], qq[1][i]);
if(!bl[qq[0][i]]){
qqq[p] = qq[0][i];
p++;
}
bl[qq[0][i]] = 1;
}
for(int i = 0; i < p; i++){
memset(dis, 31, sizeof dis);
u = qqq[i];
dis[u] = 0;
bl[u] = 1;
for(int j = u; j < mx[u]; j++){
for(pii l : g[j]){
dis[l.F] = min(dis[l.F], l.S + dis[j]);
}
}
for(int j = 0; j < pt[u]; j++){
w = Q[u][j];
v = qq[1][w];
if(dis[v] >= inf)
ans[w] = -1;
else
ans[w] = dis[v];
}
}
for(int i = 1; i <= q; i++){
cout << ans[i] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2135 ms |
43532 KB |
Output is correct |
2 |
Correct |
2 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1632 KB |
Output is correct |
4 |
Correct |
2 ms |
1748 KB |
Output is correct |
5 |
Correct |
7 ms |
4180 KB |
Output is correct |
6 |
Correct |
7 ms |
4076 KB |
Output is correct |
7 |
Correct |
7 ms |
4104 KB |
Output is correct |
8 |
Correct |
2209 ms |
43672 KB |
Output is correct |
9 |
Correct |
2339 ms |
43440 KB |
Output is correct |
10 |
Correct |
179 ms |
41196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
5072 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
3 ms |
1980 KB |
Output is correct |
8 |
Correct |
4 ms |
2004 KB |
Output is correct |
9 |
Correct |
21 ms |
4312 KB |
Output is correct |
10 |
Correct |
46 ms |
6592 KB |
Output is correct |
11 |
Correct |
38 ms |
5196 KB |
Output is correct |
12 |
Correct |
39 ms |
4516 KB |
Output is correct |
13 |
Correct |
57 ms |
6920 KB |
Output is correct |
14 |
Correct |
30 ms |
4840 KB |
Output is correct |
15 |
Correct |
28 ms |
4268 KB |
Output is correct |
16 |
Correct |
24 ms |
4172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1636 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
2 ms |
1748 KB |
Output is correct |
5 |
Correct |
2 ms |
1620 KB |
Output is correct |
6 |
Correct |
3 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
2132 KB |
Output is correct |
8 |
Correct |
4 ms |
2260 KB |
Output is correct |
9 |
Correct |
4 ms |
2208 KB |
Output is correct |
10 |
Correct |
50 ms |
5272 KB |
Output is correct |
11 |
Correct |
73 ms |
5904 KB |
Output is correct |
12 |
Correct |
84 ms |
7616 KB |
Output is correct |
13 |
Correct |
76 ms |
8000 KB |
Output is correct |
14 |
Correct |
78 ms |
6832 KB |
Output is correct |
15 |
Correct |
48 ms |
5236 KB |
Output is correct |
16 |
Correct |
41 ms |
5232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1636 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
2 ms |
1748 KB |
Output is correct |
5 |
Correct |
2 ms |
1620 KB |
Output is correct |
6 |
Correct |
3 ms |
2132 KB |
Output is correct |
7 |
Correct |
3 ms |
2132 KB |
Output is correct |
8 |
Correct |
4 ms |
2260 KB |
Output is correct |
9 |
Correct |
4 ms |
2208 KB |
Output is correct |
10 |
Correct |
50 ms |
5272 KB |
Output is correct |
11 |
Correct |
73 ms |
5904 KB |
Output is correct |
12 |
Correct |
84 ms |
7616 KB |
Output is correct |
13 |
Correct |
76 ms |
8000 KB |
Output is correct |
14 |
Correct |
78 ms |
6832 KB |
Output is correct |
15 |
Correct |
48 ms |
5236 KB |
Output is correct |
16 |
Correct |
41 ms |
5232 KB |
Output is correct |
17 |
Correct |
912 ms |
20256 KB |
Output is correct |
18 |
Correct |
1 ms |
1748 KB |
Output is correct |
19 |
Correct |
1 ms |
1632 KB |
Output is correct |
20 |
Correct |
2 ms |
1632 KB |
Output is correct |
21 |
Correct |
1 ms |
1748 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
11 ms |
5308 KB |
Output is correct |
24 |
Correct |
12 ms |
5224 KB |
Output is correct |
25 |
Correct |
17 ms |
5312 KB |
Output is correct |
26 |
Correct |
14 ms |
5204 KB |
Output is correct |
27 |
Correct |
834 ms |
19424 KB |
Output is correct |
28 |
Correct |
1251 ms |
21772 KB |
Output is correct |
29 |
Correct |
1175 ms |
22456 KB |
Output is correct |
30 |
Correct |
1085 ms |
21020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2135 ms |
43532 KB |
Output is correct |
2 |
Correct |
2 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1632 KB |
Output is correct |
4 |
Correct |
2 ms |
1748 KB |
Output is correct |
5 |
Correct |
7 ms |
4180 KB |
Output is correct |
6 |
Correct |
7 ms |
4076 KB |
Output is correct |
7 |
Correct |
7 ms |
4104 KB |
Output is correct |
8 |
Correct |
2209 ms |
43672 KB |
Output is correct |
9 |
Correct |
2339 ms |
43440 KB |
Output is correct |
10 |
Correct |
179 ms |
41196 KB |
Output is correct |
11 |
Correct |
53 ms |
5072 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
1 ms |
1620 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1620 KB |
Output is correct |
17 |
Correct |
3 ms |
1980 KB |
Output is correct |
18 |
Correct |
4 ms |
2004 KB |
Output is correct |
19 |
Correct |
21 ms |
4312 KB |
Output is correct |
20 |
Correct |
46 ms |
6592 KB |
Output is correct |
21 |
Correct |
38 ms |
5196 KB |
Output is correct |
22 |
Correct |
39 ms |
4516 KB |
Output is correct |
23 |
Correct |
57 ms |
6920 KB |
Output is correct |
24 |
Correct |
30 ms |
4840 KB |
Output is correct |
25 |
Correct |
28 ms |
4268 KB |
Output is correct |
26 |
Correct |
24 ms |
4172 KB |
Output is correct |
27 |
Correct |
1 ms |
1636 KB |
Output is correct |
28 |
Correct |
1 ms |
1748 KB |
Output is correct |
29 |
Correct |
1 ms |
1628 KB |
Output is correct |
30 |
Correct |
2 ms |
1748 KB |
Output is correct |
31 |
Correct |
2 ms |
1620 KB |
Output is correct |
32 |
Correct |
3 ms |
2132 KB |
Output is correct |
33 |
Correct |
3 ms |
2132 KB |
Output is correct |
34 |
Correct |
4 ms |
2260 KB |
Output is correct |
35 |
Correct |
4 ms |
2208 KB |
Output is correct |
36 |
Correct |
50 ms |
5272 KB |
Output is correct |
37 |
Correct |
73 ms |
5904 KB |
Output is correct |
38 |
Correct |
84 ms |
7616 KB |
Output is correct |
39 |
Correct |
76 ms |
8000 KB |
Output is correct |
40 |
Correct |
78 ms |
6832 KB |
Output is correct |
41 |
Correct |
48 ms |
5236 KB |
Output is correct |
42 |
Correct |
41 ms |
5232 KB |
Output is correct |
43 |
Correct |
912 ms |
20256 KB |
Output is correct |
44 |
Correct |
1 ms |
1748 KB |
Output is correct |
45 |
Correct |
1 ms |
1632 KB |
Output is correct |
46 |
Correct |
2 ms |
1632 KB |
Output is correct |
47 |
Correct |
1 ms |
1748 KB |
Output is correct |
48 |
Correct |
1 ms |
1620 KB |
Output is correct |
49 |
Correct |
11 ms |
5308 KB |
Output is correct |
50 |
Correct |
12 ms |
5224 KB |
Output is correct |
51 |
Correct |
17 ms |
5312 KB |
Output is correct |
52 |
Correct |
14 ms |
5204 KB |
Output is correct |
53 |
Correct |
834 ms |
19424 KB |
Output is correct |
54 |
Correct |
1251 ms |
21772 KB |
Output is correct |
55 |
Correct |
1175 ms |
22456 KB |
Output is correct |
56 |
Correct |
1085 ms |
21020 KB |
Output is correct |
57 |
Execution timed out |
3016 ms |
47304 KB |
Time limit exceeded |
58 |
Halted |
0 ms |
0 KB |
- |