#include <bits/stdc++.h>
#define DIM 50010
#define INF 2000000000
using namespace std;
int k,n,m,q,i,j,x,y,el,cost,nr;
int aint[DIM*4][6][6],sol[6][6],aux[6][6],intv[DIM][6][6];
vector <pair<int,int> > L[DIM];
void init (int a[][6]){
for (int i=0;i<k;i++)
for (int j=0;j<k;j++)
a[i][j] = INF;
}
void build (int nod, int st, int dr){
if (st == dr){
/// practic eu in nod tin minte costul sa ajung din st in st+1
for (int i=st*k;i<min((st+1)*k,n);i++){
for (auto it:L[i]){
int j = it.first, cost = it.second;
aint[nod][i%k][j%k] = min(aint[nod][i%k][j%k],cost);
}
}
return;
}
int mid = (st+dr)>>1;
build (nod<<1,st,mid);
build ((nod<<1)|1,mid+1,dr);
int fiu_st = nod<<1, fiu_dr = (nod<<1)|1;
/// vr sa combin doua intervale, st mid si mid+1 dr
/// costul de ajung din st in dr+1!!
for (int i=0;i<k;i++)
for (int j=0;j<k;j++)
for (int t=0;t<k;t++)
if (aint[fiu_st][i][t] != INF && aint[fiu_dr][t][j] != INF)
aint[nod][i][j] = min (aint[nod][i][j],aint[fiu_st][i][t] + aint[fiu_dr][t][j]);
}
void query (int nod, int st, int dr, int x, int y){
if (x <= st && dr <= y){
el++;
for (int i=0;i<k;i++)
for (int j=0;j<k;j++)
intv[el][i][j] = aint[nod][i][j];
return;
}
int mid = (st+dr)>>1;
if (x <= mid)
query (nod<<1,st,mid,x,y);
if (y > mid)
query ((nod<<1)|1,mid+1,dr,x,y);
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>k>>n>>m>>q;
for (i=1;i<=m;i++){
cin>>x>>y>>cost;
L[x].push_back(make_pair(y,cost));
}
nr = (n-1)/k;
for (i=1;i<=4*nr;i++)
init (aint[i]);
build (1,0,nr);
for (;q--;){
cin>>x>>y;
if (x/k >= y/k){
cout<<-1<<"\n";
continue;
}
el = 0;
query (1,0,nr,x/k,y/k-1);
/// pt primul interval un rest il am deja stabilit
init(sol);
//for (i=0;i<k;i++)
// sol[x%k][i] = intv[1][x%k][i];
for (int i=0;i<k;i++)
for (int j=0;j<k;j++)
sol[i][j] = intv[1][i][j];
for (int idx=2;idx<=el;idx++){
init (aux);
for (int i=0;i<k;i++)
for (int j=0;j<k;j++)
for (int t=0;t<k;t++)
if (sol[i][t] != INF && intv[idx][t][j] != INF)
aux[i][j] = min (aux[i][j],sol[i][t] + intv[idx][t][j]);
for (int i=0;i<k;i++)
for (int j=0;j<k;j++)
sol[i][j] = aux[i][j];
}
if (sol[x%k][y%k] == INF)
cout<<-1<<"\n";
else cout<<sol[x%k][y%k]<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
31608 KB |
Output is correct |
2 |
Correct |
3 ms |
1528 KB |
Output is correct |
3 |
Correct |
3 ms |
1528 KB |
Output is correct |
4 |
Correct |
2 ms |
1528 KB |
Output is correct |
5 |
Correct |
8 ms |
2168 KB |
Output is correct |
6 |
Correct |
8 ms |
2168 KB |
Output is correct |
7 |
Correct |
8 ms |
2168 KB |
Output is correct |
8 |
Correct |
153 ms |
31480 KB |
Output is correct |
9 |
Correct |
152 ms |
31448 KB |
Output is correct |
10 |
Correct |
81 ms |
30032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
17528 KB |
Output is correct |
2 |
Correct |
3 ms |
1532 KB |
Output is correct |
3 |
Correct |
4 ms |
1528 KB |
Output is correct |
4 |
Correct |
3 ms |
1532 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
3 ms |
1528 KB |
Output is correct |
7 |
Correct |
40 ms |
2168 KB |
Output is correct |
8 |
Correct |
42 ms |
2040 KB |
Output is correct |
9 |
Correct |
149 ms |
32212 KB |
Output is correct |
10 |
Correct |
295 ms |
15752 KB |
Output is correct |
11 |
Correct |
220 ms |
19064 KB |
Output is correct |
12 |
Correct |
181 ms |
13744 KB |
Output is correct |
13 |
Correct |
267 ms |
9976 KB |
Output is correct |
14 |
Correct |
166 ms |
10360 KB |
Output is correct |
15 |
Correct |
148 ms |
7388 KB |
Output is correct |
16 |
Correct |
153 ms |
7496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
5 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 |
2168 KB |
Output is correct |
7 |
Correct |
6 ms |
1832 KB |
Output is correct |
8 |
Correct |
10 ms |
1784 KB |
Output is correct |
9 |
Correct |
9 ms |
1764 KB |
Output is correct |
10 |
Correct |
109 ms |
31992 KB |
Output is correct |
11 |
Correct |
174 ms |
18680 KB |
Output is correct |
12 |
Correct |
238 ms |
15480 KB |
Output is correct |
13 |
Correct |
265 ms |
16068 KB |
Output is correct |
14 |
Correct |
200 ms |
14840 KB |
Output is correct |
15 |
Correct |
132 ms |
7336 KB |
Output is correct |
16 |
Correct |
132 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
5 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 |
2168 KB |
Output is correct |
7 |
Correct |
6 ms |
1832 KB |
Output is correct |
8 |
Correct |
10 ms |
1784 KB |
Output is correct |
9 |
Correct |
9 ms |
1764 KB |
Output is correct |
10 |
Correct |
109 ms |
31992 KB |
Output is correct |
11 |
Correct |
174 ms |
18680 KB |
Output is correct |
12 |
Correct |
238 ms |
15480 KB |
Output is correct |
13 |
Correct |
265 ms |
16068 KB |
Output is correct |
14 |
Correct |
200 ms |
14840 KB |
Output is correct |
15 |
Correct |
132 ms |
7336 KB |
Output is correct |
16 |
Correct |
132 ms |
7416 KB |
Output is correct |
17 |
Correct |
187 ms |
18840 KB |
Output is correct |
18 |
Correct |
4 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 |
1528 KB |
Output is correct |
22 |
Correct |
3 ms |
1528 KB |
Output is correct |
23 |
Correct |
16 ms |
2168 KB |
Output is correct |
24 |
Correct |
17 ms |
1912 KB |
Output is correct |
25 |
Correct |
25 ms |
1784 KB |
Output is correct |
26 |
Correct |
21 ms |
1784 KB |
Output is correct |
27 |
Correct |
123 ms |
32120 KB |
Output is correct |
28 |
Correct |
260 ms |
15568 KB |
Output is correct |
29 |
Correct |
303 ms |
16164 KB |
Output is correct |
30 |
Correct |
230 ms |
14840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
31608 KB |
Output is correct |
2 |
Correct |
3 ms |
1528 KB |
Output is correct |
3 |
Correct |
3 ms |
1528 KB |
Output is correct |
4 |
Correct |
2 ms |
1528 KB |
Output is correct |
5 |
Correct |
8 ms |
2168 KB |
Output is correct |
6 |
Correct |
8 ms |
2168 KB |
Output is correct |
7 |
Correct |
8 ms |
2168 KB |
Output is correct |
8 |
Correct |
153 ms |
31480 KB |
Output is correct |
9 |
Correct |
152 ms |
31448 KB |
Output is correct |
10 |
Correct |
81 ms |
30032 KB |
Output is correct |
11 |
Correct |
214 ms |
17528 KB |
Output is correct |
12 |
Correct |
3 ms |
1532 KB |
Output is correct |
13 |
Correct |
4 ms |
1528 KB |
Output is correct |
14 |
Correct |
3 ms |
1532 KB |
Output is correct |
15 |
Correct |
3 ms |
1528 KB |
Output is correct |
16 |
Correct |
3 ms |
1528 KB |
Output is correct |
17 |
Correct |
40 ms |
2168 KB |
Output is correct |
18 |
Correct |
42 ms |
2040 KB |
Output is correct |
19 |
Correct |
149 ms |
32212 KB |
Output is correct |
20 |
Correct |
295 ms |
15752 KB |
Output is correct |
21 |
Correct |
220 ms |
19064 KB |
Output is correct |
22 |
Correct |
181 ms |
13744 KB |
Output is correct |
23 |
Correct |
267 ms |
9976 KB |
Output is correct |
24 |
Correct |
166 ms |
10360 KB |
Output is correct |
25 |
Correct |
148 ms |
7388 KB |
Output is correct |
26 |
Correct |
153 ms |
7496 KB |
Output is correct |
27 |
Correct |
3 ms |
1528 KB |
Output is correct |
28 |
Correct |
5 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 |
2168 KB |
Output is correct |
33 |
Correct |
6 ms |
1832 KB |
Output is correct |
34 |
Correct |
10 ms |
1784 KB |
Output is correct |
35 |
Correct |
9 ms |
1764 KB |
Output is correct |
36 |
Correct |
109 ms |
31992 KB |
Output is correct |
37 |
Correct |
174 ms |
18680 KB |
Output is correct |
38 |
Correct |
238 ms |
15480 KB |
Output is correct |
39 |
Correct |
265 ms |
16068 KB |
Output is correct |
40 |
Correct |
200 ms |
14840 KB |
Output is correct |
41 |
Correct |
132 ms |
7336 KB |
Output is correct |
42 |
Correct |
132 ms |
7416 KB |
Output is correct |
43 |
Correct |
187 ms |
18840 KB |
Output is correct |
44 |
Correct |
4 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 |
1528 KB |
Output is correct |
48 |
Correct |
3 ms |
1528 KB |
Output is correct |
49 |
Correct |
16 ms |
2168 KB |
Output is correct |
50 |
Correct |
17 ms |
1912 KB |
Output is correct |
51 |
Correct |
25 ms |
1784 KB |
Output is correct |
52 |
Correct |
21 ms |
1784 KB |
Output is correct |
53 |
Correct |
123 ms |
32120 KB |
Output is correct |
54 |
Correct |
260 ms |
15568 KB |
Output is correct |
55 |
Correct |
303 ms |
16164 KB |
Output is correct |
56 |
Correct |
230 ms |
14840 KB |
Output is correct |
57 |
Correct |
414 ms |
14664 KB |
Output is correct |
58 |
Correct |
157 ms |
32236 KB |
Output is correct |
59 |
Correct |
229 ms |
18936 KB |
Output is correct |