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>
#pragma GCC target("sse,sse2")
#pragma GCC optimize("unroll-loops,O2")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 5e4+10,mod = 998244353,inf = 1e9+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
int n,m,k;
vector<pll> adj[N];
int a[N][6][6];
int seg[N*4][6][6];
int co[5][5],cur[5][5];
vector<int> ve;
void build(int l,int r,int v = 1){
if (r-l == 1){
rep(i,0,k){
rep(j,0,k){
seg[v][i][j] = a[l][i][j];
}
}
return;
}
int u = (v << 1),m = (l+r) >> 1;
build(l,m,u);
build(m,r,u|1);
rep(i,0,k){
rep(j,0,k){
rep(x,0,k){
seg[v][i][j] = min(seg[v][i][j],seg[u][i][x]+seg[u|1][x][j]);
}
}
}
}
void que(int l,int r,int s,int e,int v = 1){
if (s <= l && r <= e){
ve.pb(v);
return;
}
if (s >= r || l >= e) return;
int u = (v << 1),m = (l+r) >> 1;
que(l,m,s,e,u);
que(m,r,s,e,u|1);
}
int main(){
// ios :: sync_with_stdio(0); cin.tie(0);
memset(seg,63,sizeof seg);
memset(a,63,sizeof a);
int q;
cin >> k >> n >> m >> q;
rep(i,0,m){
int u,v,w;
cin >> u >> v >> w;
adj[u].pb({v,w});
}
rep(i,0,n){
for (pll p : adj[i]){
int x = i%k,y = p.X%k;
a[i/k][x][y] = min(a[i/k][x][y],p.Y);
}
}
int sz = (n-1)/k;
build(0,sz+1);
while (q--){
int x,y;
cin >> x >> y;
if (x/k == y/k){
cout << -1 << endl;
continue;
}
ve.clear();
que(0,sz+1,x/k,y/k);
bool f = 0;
for (int v : ve){
if (!f){
rep(i,0,k)
rep(j,0,k)
cur[i][j] = seg[v][i][j];
f = 1;
continue;
}
memset(co,63,sizeof co);
rep(i,0,k){
rep(j,0,k){
rep(z,0,k){
co[i][j] = min(co[i][j],cur[i][z]+seg[v][z][j]);
}
}
}
rep(i,0,k) rep(j,0,k) cur[i][j] = co[i][j];
}
if (cur[x%k][y%k] > inf) cout << -1 << endl;
else cout << cur[x%k][y%k] << endl;
}
return 0;
}
# | 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... |