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>
#include <cmath>
typedef long long ll;
using namespace std;
ll inf = 1e9+12;
const int N = 1e4+1;
vector<vector<vector<ll>>> L(N);
vector<vector<vector<ll>>> R(N);
vector<vector<pair<int,int>>> adj(N,vector<pair<int,int>>(0));
vector<vector<pair<int,int>>> radj(N,vector<pair<int,int>>(0));
ll ans[N];
int A[N];
int B[N];
int n,k;
void divi(int l, int r, vector<int> v) {
if (!v.size()) return;
if (l == r) {
for(int i : v) ans[i] = 0;
}
int m = (l+r)/2;
int mid = m;
m/=k;
for(int i = m+1; i < r/k+1; i++) {
for(int j = 0; j < k; j++) {
for(pair<int,int> x : radj[i*k+j]) {
for(int f = 0; f < k; f++) {
R[i-m][f][j] = min(R[i-m][f][j],x.second+R[i-m-1][f][x.first%k]);
}
}
}
}
for(int i = m-1; i > l/k-1; i--) {
for(int j = 0; j < k; j++) {
for(pair<int,int> x : adj[i*k+j]) {
for(int f = 0; f < k; f++) {
L[m-i][f][j] = min(L[m-i][f][j],x.second+L[m-i-1][f][x.first%k]);
}
}
}
}
vector<int> todo[2];
for(int i : v) {
int a = A[i],b=B[i];
if (a <= mid && b > mid) {
int ka = a/k;
int kb = b/k;
for(int j = 0; j < k; j++) {
ans[i] = min(ans[i],L[m-ka][j][a%k]+R[kb-m][j][b%k]);
}
if (ans[i] >= inf) ans[i] = -1;
continue;
}
todo[a > mid].push_back(i);
}
divi(l,mid,todo[0]); divi(mid+1,r,todo[1]);
for(int i = m+1; i < r/k+1; i++) {
for(int j = 0; j < k; j++) {
for(int f = 0; f < k; f++) {
R[i-m][f][j] = inf;
}
}
}
for(int i = m-1; i > l/k-1; i--) {
for(int j = 0; j < k; j++) {
for(int f = 0; f < k; f++) {
L[m-i][f][j] = inf;
}
}
}
}
int main() {
int q; int m; cin >> k >> n >> m >> q;
int a,b,t;
for(int i = 0; i < n; i++) {
ans[i] = inf;
L[i] = vector<vector<ll>>(k,vector<ll>(k,inf));
R[i] = vector<vector<ll>>(k,vector<ll>(k,inf));
}
for(int i = 0; i < m; i++) {
cin >> a >> b >> t;
adj[a].push_back({b,t});
radj[b].push_back({a,t});
}
vector<int> qs;
for(int i = 0; i < q; i++) {
cin >> a >> b;
A[i] = a; B[i] = b;
qs.push_back(i);
}
for(int i = 0; i < k; i++) {
L[0][i][i] = 0;
R[0][i][i] = 0;
}
divi(0,n-1,qs);
for(int i = 0; i < q; i++) {
cout << ans[i] << endl;
}
}
# | 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... |