This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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;
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |