#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=5e4+10;
const ll inf=1e9;
const ll mod=1e9+7;
ll k;
struct node
{
ll dp[5][5];
node()
{
for(int i=0;i<k;i++)
{
for(int j=0;j<k;j++) dp[i][j]=inf;
}
}
node operator+(const node&o)
{
node ans=node();
for(int i=0;i<k;i++)
{
for(int j=0;j<k;j++)
{
for(int q=0;q<k;q++)
{
ans.dp[i][j]=min(ans.dp[i][j],dp[i][q]+o.dp[q][j]);
}
}
}
return ans;
}
}st[4*maxN];
ll x;
vector<pli>g[maxN];
void build(ll id=1,ll l=1,ll r=x)
{
if(l==r)
{
for(int i=(l-1)*k;i<l*k;i++)
{
for(int j=l*k;j<(l+1)*k;j++)
{
st[id].dp[i%k][j%k]=inf;
}
}
for(int i=(l-1)*k;i<l*k;i++)
{
for(auto zz:g[i])
{
st[id].dp[i%k][zz.fi%k]=min(st[id].dp[i%k][zz.fi%k],zz.se);
}
}
return;
}
ll mid=l+r>>1;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
st[id]=st[id*2]+st[id*2+1];
}
bool inter(ll i,ll j,ll l,ll r)
{
if(j<l||r<i) return false;
return true;
}
node get(ll i,ll j,ll id=1,ll l=1,ll r=x)
{
if(i<=l&&r<=j) return st[id];
ll mid=l+r>>1;
if(inter(i,j,l,mid))
{
if(inter(i,j,mid+1,r))
{
return get(i,j,id*2,l,mid)+get(i,j,id*2+1,mid+1,r);
}
else return get(i,j,id*2,l,mid);
}
else
{
return get(i,j,id*2+1,mid+1,r);
}
}
ll n,m,o;
void solve()
{
cin >> k >> n >> m >>o;
for(int i=1;i<=m;i++)
{
ll u,v,w;
cin >> u >> v >> w;
g[u].pb({v,w});
}
x=(n-1)/k;
build();
for(int i=1;i<=o;i++)
{
ll a,b;
cin >> a >> b;
if(a==b)
{
cout << 0 << '\n';
}
else if(a/k==b/k)
{
cout << -1 << '\n';
}
else
{
auto cc=get(a/k +1 ,b/k);
if(cc.dp[a%k][b%k]!=inf)
{
cout << cc.dp[a%k][b%k]<<'\n';
}
else
{
cout <<-1<<'\n';
}
}
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
toll.cpp: In function 'void build(ll, ll, ll)':
toll.cpp:62:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | ll mid=l+r>>1;
| ~^~
toll.cpp: In function 'node get(ll, ll, ll, ll, ll)':
toll.cpp:75:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | ll mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
29696 KB |
Output is correct |
2 |
Correct |
1 ms |
1508 KB |
Output is correct |
3 |
Correct |
1 ms |
1512 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
3 ms |
1992 KB |
Output is correct |
8 |
Correct |
51 ms |
29644 KB |
Output is correct |
9 |
Correct |
40 ms |
29464 KB |
Output is correct |
10 |
Correct |
22 ms |
27276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
18412 KB |
Output is correct |
2 |
Correct |
2 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1504 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
5 ms |
2040 KB |
Output is correct |
8 |
Correct |
5 ms |
1864 KB |
Output is correct |
9 |
Correct |
38 ms |
29600 KB |
Output is correct |
10 |
Correct |
61 ms |
15232 KB |
Output is correct |
11 |
Correct |
49 ms |
18672 KB |
Output is correct |
12 |
Correct |
40 ms |
12332 KB |
Output is correct |
13 |
Correct |
57 ms |
11800 KB |
Output is correct |
14 |
Correct |
41 ms |
11980 KB |
Output is correct |
15 |
Correct |
38 ms |
7952 KB |
Output is correct |
16 |
Correct |
32 ms |
7884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1512 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1748 KB |
Output is correct |
8 |
Correct |
3 ms |
1748 KB |
Output is correct |
9 |
Correct |
4 ms |
1748 KB |
Output is correct |
10 |
Correct |
38 ms |
29404 KB |
Output is correct |
11 |
Correct |
49 ms |
18300 KB |
Output is correct |
12 |
Correct |
55 ms |
15080 KB |
Output is correct |
13 |
Correct |
62 ms |
15712 KB |
Output is correct |
14 |
Correct |
45 ms |
13900 KB |
Output is correct |
15 |
Correct |
29 ms |
7852 KB |
Output is correct |
16 |
Correct |
32 ms |
7868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1512 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1748 KB |
Output is correct |
8 |
Correct |
3 ms |
1748 KB |
Output is correct |
9 |
Correct |
4 ms |
1748 KB |
Output is correct |
10 |
Correct |
38 ms |
29404 KB |
Output is correct |
11 |
Correct |
49 ms |
18300 KB |
Output is correct |
12 |
Correct |
55 ms |
15080 KB |
Output is correct |
13 |
Correct |
62 ms |
15712 KB |
Output is correct |
14 |
Correct |
45 ms |
13900 KB |
Output is correct |
15 |
Correct |
29 ms |
7852 KB |
Output is correct |
16 |
Correct |
32 ms |
7868 KB |
Output is correct |
17 |
Correct |
53 ms |
18412 KB |
Output is correct |
18 |
Correct |
1 ms |
1492 KB |
Output is correct |
19 |
Correct |
2 ms |
1492 KB |
Output is correct |
20 |
Correct |
1 ms |
1492 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
1492 KB |
Output is correct |
23 |
Correct |
3 ms |
1908 KB |
Output is correct |
24 |
Correct |
3 ms |
1776 KB |
Output is correct |
25 |
Correct |
6 ms |
1748 KB |
Output is correct |
26 |
Correct |
4 ms |
1780 KB |
Output is correct |
27 |
Correct |
34 ms |
29516 KB |
Output is correct |
28 |
Correct |
59 ms |
15100 KB |
Output is correct |
29 |
Correct |
62 ms |
15860 KB |
Output is correct |
30 |
Correct |
53 ms |
14068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
29696 KB |
Output is correct |
2 |
Correct |
1 ms |
1508 KB |
Output is correct |
3 |
Correct |
1 ms |
1512 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
3 ms |
1992 KB |
Output is correct |
8 |
Correct |
51 ms |
29644 KB |
Output is correct |
9 |
Correct |
40 ms |
29464 KB |
Output is correct |
10 |
Correct |
22 ms |
27276 KB |
Output is correct |
11 |
Correct |
48 ms |
18412 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1504 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
5 ms |
2040 KB |
Output is correct |
18 |
Correct |
5 ms |
1864 KB |
Output is correct |
19 |
Correct |
38 ms |
29600 KB |
Output is correct |
20 |
Correct |
61 ms |
15232 KB |
Output is correct |
21 |
Correct |
49 ms |
18672 KB |
Output is correct |
22 |
Correct |
40 ms |
12332 KB |
Output is correct |
23 |
Correct |
57 ms |
11800 KB |
Output is correct |
24 |
Correct |
41 ms |
11980 KB |
Output is correct |
25 |
Correct |
38 ms |
7952 KB |
Output is correct |
26 |
Correct |
32 ms |
7884 KB |
Output is correct |
27 |
Correct |
1 ms |
1512 KB |
Output is correct |
28 |
Correct |
1 ms |
1492 KB |
Output is correct |
29 |
Correct |
1 ms |
1492 KB |
Output is correct |
30 |
Correct |
1 ms |
1492 KB |
Output is correct |
31 |
Correct |
1 ms |
1492 KB |
Output is correct |
32 |
Correct |
2 ms |
1876 KB |
Output is correct |
33 |
Correct |
2 ms |
1748 KB |
Output is correct |
34 |
Correct |
3 ms |
1748 KB |
Output is correct |
35 |
Correct |
4 ms |
1748 KB |
Output is correct |
36 |
Correct |
38 ms |
29404 KB |
Output is correct |
37 |
Correct |
49 ms |
18300 KB |
Output is correct |
38 |
Correct |
55 ms |
15080 KB |
Output is correct |
39 |
Correct |
62 ms |
15712 KB |
Output is correct |
40 |
Correct |
45 ms |
13900 KB |
Output is correct |
41 |
Correct |
29 ms |
7852 KB |
Output is correct |
42 |
Correct |
32 ms |
7868 KB |
Output is correct |
43 |
Correct |
53 ms |
18412 KB |
Output is correct |
44 |
Correct |
1 ms |
1492 KB |
Output is correct |
45 |
Correct |
2 ms |
1492 KB |
Output is correct |
46 |
Correct |
1 ms |
1492 KB |
Output is correct |
47 |
Correct |
1 ms |
1492 KB |
Output is correct |
48 |
Correct |
1 ms |
1492 KB |
Output is correct |
49 |
Correct |
3 ms |
1908 KB |
Output is correct |
50 |
Correct |
3 ms |
1776 KB |
Output is correct |
51 |
Correct |
6 ms |
1748 KB |
Output is correct |
52 |
Correct |
4 ms |
1780 KB |
Output is correct |
53 |
Correct |
34 ms |
29516 KB |
Output is correct |
54 |
Correct |
59 ms |
15100 KB |
Output is correct |
55 |
Correct |
62 ms |
15860 KB |
Output is correct |
56 |
Correct |
53 ms |
14068 KB |
Output is correct |
57 |
Correct |
99 ms |
15620 KB |
Output is correct |
58 |
Correct |
40 ms |
29544 KB |
Output is correct |
59 |
Correct |
58 ms |
18640 KB |
Output is correct |