#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5;
const ll INF = 1e15;
int K, N, M, Q;
vector<pii> adj[MAXN+10];
struct Node
{
vector<vector<ll>> V;
Node() { V=vector<vector<ll>>(K, vector<ll>(K, INF)); }
};
Node Z;
Node operator + (const Node &p, const Node &q)
{
Node ret=Node();
int i, j, k;
for(i=0; i<K; i++)
{
for(j=0; j<K; j++)
{
ll val=INF;
for(k=0; k<K; k++) val=min(val, p.V[i][k]+q.V[k][j]);
ret.V[i][j]=val;
}
}
return ret;
}
Node tree[MAXN*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
int i, j;
tree[node]=Node();
for(i=(tl-1)*K; i<tl*K; i++) for(auto nxt : adj[i]) tree[node].V[i%K][nxt.first%K]=nxt.second;
//printf("%d %d : \n", tl, tr);
//for(i=0; i<K; i++) { for(j=0; j<K; j++) printf("%lld ", tree[node].V[i][j]); printf("\n"); }
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node]=tree[node*2]+tree[node*2+1];
//int i, j;
//printf("%d %d : \n", tl, tr);
//for(i=0; i<K; i++) { for(j=0; j<K; j++) printf("%lld ", tree[node].V[i][j]); printf("\n"); }
}
Node query(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return tree[node];
if(tr<l || r<tl) return Z;
int mid=tl+tr>>1;
return query(node*2, tl, mid, l, r)+query(node*2+1, mid+1, tr, l, r);
}
int main()
{
int i, j, k;
scanf("%d%d%d%d", &K, &N, &M, &Q);
if(N%K) N=N/K*K+K;
Z=Node();
for(i=0; i<K; i++) Z.V[i][i]=0;
for(i=1; i<=M; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
adj[u].push_back({v, w});
}
init(1, 1, N/K-1);
while(Q--)
{
int a, b;
scanf("%d%d", &a, &b);
if(a/K==b/K) { printf("-1\n"); continue; }
Node t=query(1, 1, N/K-1, a/K+1, b/K);
ll ans=t.V[a%K][b%K];
if(ans>=INF) ans=-1;
printf("%lld\n", ans);
}
}
Compilation message
toll.cpp: In function 'void init(int, int, int)':
toll.cpp:43:10: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
toll.cpp:50:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
toll.cpp: In function 'Node query(int, int, int, int, int)':
toll.cpp:63:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
toll.cpp: In function 'int main()':
toll.cpp:69:9: warning: unused variable 'j' [-Wunused-variable]
int i, j, k;
^
toll.cpp:69:12: warning: unused variable 'k' [-Wunused-variable]
int i, j, k;
^
toll.cpp:70:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &K, &N, &M, &Q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &u, &v, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
66964 KB |
Output is correct |
2 |
Correct |
64 ms |
59000 KB |
Output is correct |
3 |
Correct |
64 ms |
59000 KB |
Output is correct |
4 |
Correct |
64 ms |
59000 KB |
Output is correct |
5 |
Correct |
78 ms |
59256 KB |
Output is correct |
6 |
Correct |
83 ms |
59132 KB |
Output is correct |
7 |
Correct |
80 ms |
59128 KB |
Output is correct |
8 |
Correct |
191 ms |
67800 KB |
Output is correct |
9 |
Correct |
199 ms |
67832 KB |
Output is correct |
10 |
Correct |
162 ms |
65528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
66928 KB |
Output is correct |
2 |
Correct |
64 ms |
59000 KB |
Output is correct |
3 |
Correct |
63 ms |
59000 KB |
Output is correct |
4 |
Correct |
119 ms |
59008 KB |
Output is correct |
5 |
Correct |
81 ms |
59128 KB |
Output is correct |
6 |
Correct |
65 ms |
59068 KB |
Output is correct |
7 |
Correct |
90 ms |
59460 KB |
Output is correct |
8 |
Correct |
104 ms |
59452 KB |
Output is correct |
9 |
Correct |
188 ms |
67696 KB |
Output is correct |
10 |
Correct |
242 ms |
69368 KB |
Output is correct |
11 |
Correct |
193 ms |
68600 KB |
Output is correct |
12 |
Correct |
191 ms |
67516 KB |
Output is correct |
13 |
Correct |
209 ms |
68316 KB |
Output is correct |
14 |
Correct |
166 ms |
65412 KB |
Output is correct |
15 |
Correct |
165 ms |
65912 KB |
Output is correct |
16 |
Correct |
145 ms |
65884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
59080 KB |
Output is correct |
2 |
Correct |
64 ms |
59000 KB |
Output is correct |
3 |
Correct |
64 ms |
59128 KB |
Output is correct |
4 |
Correct |
123 ms |
59032 KB |
Output is correct |
5 |
Correct |
62 ms |
59000 KB |
Output is correct |
6 |
Correct |
65 ms |
59256 KB |
Output is correct |
7 |
Correct |
65 ms |
59256 KB |
Output is correct |
8 |
Correct |
68 ms |
59312 KB |
Output is correct |
9 |
Correct |
67 ms |
59304 KB |
Output is correct |
10 |
Correct |
121 ms |
66808 KB |
Output is correct |
11 |
Correct |
137 ms |
66808 KB |
Output is correct |
12 |
Correct |
161 ms |
66976 KB |
Output is correct |
13 |
Correct |
169 ms |
67432 KB |
Output is correct |
14 |
Correct |
171 ms |
66596 KB |
Output is correct |
15 |
Correct |
120 ms |
64632 KB |
Output is correct |
16 |
Correct |
129 ms |
64808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
59080 KB |
Output is correct |
2 |
Correct |
64 ms |
59000 KB |
Output is correct |
3 |
Correct |
64 ms |
59128 KB |
Output is correct |
4 |
Correct |
123 ms |
59032 KB |
Output is correct |
5 |
Correct |
62 ms |
59000 KB |
Output is correct |
6 |
Correct |
65 ms |
59256 KB |
Output is correct |
7 |
Correct |
65 ms |
59256 KB |
Output is correct |
8 |
Correct |
68 ms |
59312 KB |
Output is correct |
9 |
Correct |
67 ms |
59304 KB |
Output is correct |
10 |
Correct |
121 ms |
66808 KB |
Output is correct |
11 |
Correct |
137 ms |
66808 KB |
Output is correct |
12 |
Correct |
161 ms |
66976 KB |
Output is correct |
13 |
Correct |
169 ms |
67432 KB |
Output is correct |
14 |
Correct |
171 ms |
66596 KB |
Output is correct |
15 |
Correct |
120 ms |
64632 KB |
Output is correct |
16 |
Correct |
129 ms |
64808 KB |
Output is correct |
17 |
Correct |
172 ms |
66816 KB |
Output is correct |
18 |
Correct |
64 ms |
59004 KB |
Output is correct |
19 |
Correct |
72 ms |
59128 KB |
Output is correct |
20 |
Correct |
74 ms |
59000 KB |
Output is correct |
21 |
Correct |
64 ms |
59000 KB |
Output is correct |
22 |
Correct |
64 ms |
59000 KB |
Output is correct |
23 |
Correct |
76 ms |
59292 KB |
Output is correct |
24 |
Correct |
79 ms |
59128 KB |
Output is correct |
25 |
Correct |
91 ms |
59256 KB |
Output is correct |
26 |
Correct |
93 ms |
59372 KB |
Output is correct |
27 |
Correct |
165 ms |
66896 KB |
Output is correct |
28 |
Correct |
201 ms |
67140 KB |
Output is correct |
29 |
Correct |
209 ms |
67292 KB |
Output is correct |
30 |
Correct |
183 ms |
66808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
66964 KB |
Output is correct |
2 |
Correct |
64 ms |
59000 KB |
Output is correct |
3 |
Correct |
64 ms |
59000 KB |
Output is correct |
4 |
Correct |
64 ms |
59000 KB |
Output is correct |
5 |
Correct |
78 ms |
59256 KB |
Output is correct |
6 |
Correct |
83 ms |
59132 KB |
Output is correct |
7 |
Correct |
80 ms |
59128 KB |
Output is correct |
8 |
Correct |
191 ms |
67800 KB |
Output is correct |
9 |
Correct |
199 ms |
67832 KB |
Output is correct |
10 |
Correct |
162 ms |
65528 KB |
Output is correct |
11 |
Correct |
196 ms |
66928 KB |
Output is correct |
12 |
Correct |
64 ms |
59000 KB |
Output is correct |
13 |
Correct |
63 ms |
59000 KB |
Output is correct |
14 |
Correct |
119 ms |
59008 KB |
Output is correct |
15 |
Correct |
81 ms |
59128 KB |
Output is correct |
16 |
Correct |
65 ms |
59068 KB |
Output is correct |
17 |
Correct |
90 ms |
59460 KB |
Output is correct |
18 |
Correct |
104 ms |
59452 KB |
Output is correct |
19 |
Correct |
188 ms |
67696 KB |
Output is correct |
20 |
Correct |
242 ms |
69368 KB |
Output is correct |
21 |
Correct |
193 ms |
68600 KB |
Output is correct |
22 |
Correct |
191 ms |
67516 KB |
Output is correct |
23 |
Correct |
209 ms |
68316 KB |
Output is correct |
24 |
Correct |
166 ms |
65412 KB |
Output is correct |
25 |
Correct |
165 ms |
65912 KB |
Output is correct |
26 |
Correct |
145 ms |
65884 KB |
Output is correct |
27 |
Correct |
72 ms |
59080 KB |
Output is correct |
28 |
Correct |
64 ms |
59000 KB |
Output is correct |
29 |
Correct |
64 ms |
59128 KB |
Output is correct |
30 |
Correct |
123 ms |
59032 KB |
Output is correct |
31 |
Correct |
62 ms |
59000 KB |
Output is correct |
32 |
Correct |
65 ms |
59256 KB |
Output is correct |
33 |
Correct |
65 ms |
59256 KB |
Output is correct |
34 |
Correct |
68 ms |
59312 KB |
Output is correct |
35 |
Correct |
67 ms |
59304 KB |
Output is correct |
36 |
Correct |
121 ms |
66808 KB |
Output is correct |
37 |
Correct |
137 ms |
66808 KB |
Output is correct |
38 |
Correct |
161 ms |
66976 KB |
Output is correct |
39 |
Correct |
169 ms |
67432 KB |
Output is correct |
40 |
Correct |
171 ms |
66596 KB |
Output is correct |
41 |
Correct |
120 ms |
64632 KB |
Output is correct |
42 |
Correct |
129 ms |
64808 KB |
Output is correct |
43 |
Correct |
172 ms |
66816 KB |
Output is correct |
44 |
Correct |
64 ms |
59004 KB |
Output is correct |
45 |
Correct |
72 ms |
59128 KB |
Output is correct |
46 |
Correct |
74 ms |
59000 KB |
Output is correct |
47 |
Correct |
64 ms |
59000 KB |
Output is correct |
48 |
Correct |
64 ms |
59000 KB |
Output is correct |
49 |
Correct |
76 ms |
59292 KB |
Output is correct |
50 |
Correct |
79 ms |
59128 KB |
Output is correct |
51 |
Correct |
91 ms |
59256 KB |
Output is correct |
52 |
Correct |
93 ms |
59372 KB |
Output is correct |
53 |
Correct |
165 ms |
66896 KB |
Output is correct |
54 |
Correct |
201 ms |
67140 KB |
Output is correct |
55 |
Correct |
209 ms |
67292 KB |
Output is correct |
56 |
Correct |
183 ms |
66808 KB |
Output is correct |
57 |
Correct |
349 ms |
72568 KB |
Output is correct |
58 |
Correct |
212 ms |
68004 KB |
Output is correct |
59 |
Correct |
293 ms |
68648 KB |
Output is correct |