#include <bits/stdc++.h>
#define ii pair<int, int>
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vii vector< pair<int, int> >
typedef long long ll;
using namespace std;
int K;
vector< vector< vector<int> > > mat;
vector< vector<int> > iden;
vii adj[50005];
struct node
{
vector< vector<int> > dat;
node *left, *right;
node(){}
node(vector< vector<int> > want, node *A, node *B)
{
dat.assign(K, vi(K, 0));
dat = want;
left = A, right = B;
}
};
node* build(int L, int R)
{
if(L == R) return new node(mat[L], NULL, NULL);
node* res = new node(vector< vector<int> >(K, vi(K, 0)), NULL, NULL);
res->left = build(L, (L+R)/2);
res->right = build((L+R)/2+1, R);
for(int i = 0; i< K; i++)
{
for(int j = 0; j< K; j++)
{
int best = 1e9;
for(int k = 0; k< K; k++)
{
best = min(best, res->left->dat[i][k]+res->right->dat[k][j]);
}
res->dat[i][j] = best;
}
}
return res;
}
node query(node* p, int L, int R, int i, int j)
{
//printf("it's %d %d %d %d\n", L, R, i, j);
if(i> R || j< L) return node(iden, NULL, NULL);
if(i<= L && R<= j) return *p;
node res(vector< vi >(K, vi(K, 0)), NULL, NULL);
node x = query(p->left, L, (L+R)/2, i, j);
node y = query(p->right, (L+R)/2+1, R, i, j);
//printf("here for %d %d\n", L, R);
for(int i = 0; i< K; i++)
{
for(int j = 0; j< K; j++)
{
int best = 1e9;
for(int k = 0; k< K; k++)
{
best = min(best, x.dat[i][k]+y.dat[k][j]);
}
res.dat[i][j] = best;
}
}
return res;
}
int main()
{
int n, m, q;
scanf("%d %d %d %d", &K, &n, &m, &q);
mat.assign(n/K+1, vector< vector<int> >(K, vi(K, 1e9)));
for(int i = 0; i< m; i++)
{
int a, b, w; scanf("%d %d %d", &a, &b, &w);
adj[a].pb(ii(b, w));
mat[a/K][a%K][b%K] = min(mat[a/K][a%K][b%K], w);
}
iden.assign(K, vi(K, 1e9));
for(int i = 0; i< K; i++) iden[i][i] = 0;
node *root = build(0, (n-1)/K-1);
//printf("ok\n");
while(q--)
{
int x, y; scanf("%d %d", &x, &y);
//kprintf("hihi\n");
if(x == y)
{
printf("0\n"); continue;
}
if(x> y) swap(x, y);
int x1 = x/K, x2 = x%K;
int y1 = y/K, y2 = y%K;
if(x1 == y1)
{
printf("-1\n"); continue;
}
node tmp = query(root, 0, (n-1)/K-1, x1, y1-1);
//printf("%d %d\n", x2, y2);
int res = tmp.dat[x2][y2];
if(res == 1e9) printf("-1\n");
else printf("%d\n", res);
/*
vector< vector<int>
> ans(K, vi(K, 0));
for(int p = x1; p< y1; p++)
{
vector< vector<int> > rub(K, vi(K, 1e9));
for(int i = 0; i< K; i++)
{
for(int j = 0; j< K; j++)
{
for(int k = 0; k< K; k++)
{
rub[i][j] = min(rub[i][j], ans[i][k]+mat[p][k][j]);
}
}
}
for(int i = 0; i< K; i++)
{
for(int j = 0; j< K; j++)
{
printf("%d ", rub[i][j]);
}
printf("\n");
}
for(int i = 0; i< K; i++) for(int j = 0; j< K; j++) ans[i][j] = rub[i][j];
}
int res = ans[x2][y2];
if(res == 1e9) printf("-1\n");
else printf("%d\n", res);
*/
}
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:73:10: 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:77:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a, b, w; scanf("%d %d %d", &a, &b, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:87:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y; scanf("%d %d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
19472 KB |
Output is correct |
2 |
Correct |
3 ms |
1540 KB |
Output is correct |
3 |
Correct |
3 ms |
1524 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
12 ms |
1804 KB |
Output is correct |
6 |
Correct |
12 ms |
1912 KB |
Output is correct |
7 |
Correct |
12 ms |
1784 KB |
Output is correct |
8 |
Correct |
228 ms |
19316 KB |
Output is correct |
9 |
Correct |
230 ms |
19308 KB |
Output is correct |
10 |
Correct |
188 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
17140 KB |
Output is correct |
2 |
Correct |
3 ms |
1528 KB |
Output is correct |
3 |
Correct |
3 ms |
1528 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
3 ms |
1528 KB |
Output is correct |
7 |
Correct |
47 ms |
2040 KB |
Output is correct |
8 |
Correct |
58 ms |
1920 KB |
Output is correct |
9 |
Correct |
161 ms |
19244 KB |
Output is correct |
10 |
Correct |
250 ms |
16620 KB |
Output is correct |
11 |
Correct |
205 ms |
17296 KB |
Output is correct |
12 |
Correct |
194 ms |
15012 KB |
Output is correct |
13 |
Correct |
166 ms |
12160 KB |
Output is correct |
14 |
Correct |
116 ms |
10844 KB |
Output is correct |
15 |
Correct |
116 ms |
9720 KB |
Output is correct |
16 |
Correct |
122 ms |
9740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1528 KB |
Output is correct |
3 |
Correct |
3 ms |
1528 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
5 ms |
1784 KB |
Output is correct |
7 |
Correct |
5 ms |
1784 KB |
Output is correct |
8 |
Correct |
7 ms |
1756 KB |
Output is correct |
9 |
Correct |
6 ms |
1784 KB |
Output is correct |
10 |
Correct |
86 ms |
19064 KB |
Output is correct |
11 |
Correct |
110 ms |
16972 KB |
Output is correct |
12 |
Correct |
125 ms |
16504 KB |
Output is correct |
13 |
Correct |
127 ms |
17016 KB |
Output is correct |
14 |
Correct |
106 ms |
15736 KB |
Output is correct |
15 |
Correct |
66 ms |
9720 KB |
Output is correct |
16 |
Correct |
66 ms |
9592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1528 KB |
Output is correct |
2 |
Correct |
3 ms |
1528 KB |
Output is correct |
3 |
Correct |
3 ms |
1528 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
5 ms |
1784 KB |
Output is correct |
7 |
Correct |
5 ms |
1784 KB |
Output is correct |
8 |
Correct |
7 ms |
1756 KB |
Output is correct |
9 |
Correct |
6 ms |
1784 KB |
Output is correct |
10 |
Correct |
86 ms |
19064 KB |
Output is correct |
11 |
Correct |
110 ms |
16972 KB |
Output is correct |
12 |
Correct |
125 ms |
16504 KB |
Output is correct |
13 |
Correct |
127 ms |
17016 KB |
Output is correct |
14 |
Correct |
106 ms |
15736 KB |
Output is correct |
15 |
Correct |
66 ms |
9720 KB |
Output is correct |
16 |
Correct |
66 ms |
9592 KB |
Output is correct |
17 |
Correct |
151 ms |
17108 KB |
Output is correct |
18 |
Correct |
3 ms |
1528 KB |
Output is correct |
19 |
Correct |
3 ms |
1528 KB |
Output is correct |
20 |
Correct |
3 ms |
1528 KB |
Output is correct |
21 |
Correct |
3 ms |
1572 KB |
Output is correct |
22 |
Correct |
3 ms |
1528 KB |
Output is correct |
23 |
Correct |
26 ms |
1912 KB |
Output is correct |
24 |
Correct |
32 ms |
1868 KB |
Output is correct |
25 |
Correct |
51 ms |
1912 KB |
Output is correct |
26 |
Correct |
43 ms |
1860 KB |
Output is correct |
27 |
Correct |
128 ms |
19328 KB |
Output is correct |
28 |
Correct |
193 ms |
16760 KB |
Output is correct |
29 |
Correct |
203 ms |
17016 KB |
Output is correct |
30 |
Correct |
170 ms |
15784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
19472 KB |
Output is correct |
2 |
Correct |
3 ms |
1540 KB |
Output is correct |
3 |
Correct |
3 ms |
1524 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
12 ms |
1804 KB |
Output is correct |
6 |
Correct |
12 ms |
1912 KB |
Output is correct |
7 |
Correct |
12 ms |
1784 KB |
Output is correct |
8 |
Correct |
228 ms |
19316 KB |
Output is correct |
9 |
Correct |
230 ms |
19308 KB |
Output is correct |
10 |
Correct |
188 ms |
16988 KB |
Output is correct |
11 |
Correct |
214 ms |
17140 KB |
Output is correct |
12 |
Correct |
3 ms |
1528 KB |
Output is correct |
13 |
Correct |
3 ms |
1528 KB |
Output is correct |
14 |
Correct |
3 ms |
1528 KB |
Output is correct |
15 |
Correct |
3 ms |
1528 KB |
Output is correct |
16 |
Correct |
3 ms |
1528 KB |
Output is correct |
17 |
Correct |
47 ms |
2040 KB |
Output is correct |
18 |
Correct |
58 ms |
1920 KB |
Output is correct |
19 |
Correct |
161 ms |
19244 KB |
Output is correct |
20 |
Correct |
250 ms |
16620 KB |
Output is correct |
21 |
Correct |
205 ms |
17296 KB |
Output is correct |
22 |
Correct |
194 ms |
15012 KB |
Output is correct |
23 |
Correct |
166 ms |
12160 KB |
Output is correct |
24 |
Correct |
116 ms |
10844 KB |
Output is correct |
25 |
Correct |
116 ms |
9720 KB |
Output is correct |
26 |
Correct |
122 ms |
9740 KB |
Output is correct |
27 |
Correct |
4 ms |
1528 KB |
Output is correct |
28 |
Correct |
3 ms |
1528 KB |
Output is correct |
29 |
Correct |
3 ms |
1528 KB |
Output is correct |
30 |
Correct |
3 ms |
1528 KB |
Output is correct |
31 |
Correct |
3 ms |
1528 KB |
Output is correct |
32 |
Correct |
5 ms |
1784 KB |
Output is correct |
33 |
Correct |
5 ms |
1784 KB |
Output is correct |
34 |
Correct |
7 ms |
1756 KB |
Output is correct |
35 |
Correct |
6 ms |
1784 KB |
Output is correct |
36 |
Correct |
86 ms |
19064 KB |
Output is correct |
37 |
Correct |
110 ms |
16972 KB |
Output is correct |
38 |
Correct |
125 ms |
16504 KB |
Output is correct |
39 |
Correct |
127 ms |
17016 KB |
Output is correct |
40 |
Correct |
106 ms |
15736 KB |
Output is correct |
41 |
Correct |
66 ms |
9720 KB |
Output is correct |
42 |
Correct |
66 ms |
9592 KB |
Output is correct |
43 |
Correct |
151 ms |
17108 KB |
Output is correct |
44 |
Correct |
3 ms |
1528 KB |
Output is correct |
45 |
Correct |
3 ms |
1528 KB |
Output is correct |
46 |
Correct |
3 ms |
1528 KB |
Output is correct |
47 |
Correct |
3 ms |
1572 KB |
Output is correct |
48 |
Correct |
3 ms |
1528 KB |
Output is correct |
49 |
Correct |
26 ms |
1912 KB |
Output is correct |
50 |
Correct |
32 ms |
1868 KB |
Output is correct |
51 |
Correct |
51 ms |
1912 KB |
Output is correct |
52 |
Correct |
43 ms |
1860 KB |
Output is correct |
53 |
Correct |
128 ms |
19328 KB |
Output is correct |
54 |
Correct |
193 ms |
16760 KB |
Output is correct |
55 |
Correct |
203 ms |
17016 KB |
Output is correct |
56 |
Correct |
170 ms |
15784 KB |
Output is correct |
57 |
Correct |
450 ms |
17692 KB |
Output is correct |
58 |
Correct |
228 ms |
19404 KB |
Output is correct |
59 |
Correct |
287 ms |
17320 KB |
Output is correct |