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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for (auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 200005;
int K, n, m, q;
vector<pii> g[mxN];
vector<pii> rg[mxN];
int res[mxN];
int dp[mxN][5];
void divi(int l, int r, vector<array<int,5>> upit){
if(l > r || sz(upit) == 0)return;
int mid = (l + r) / 2;
ff(i,l * K, min((r + 1) * K, n) - 1){
ff(o,0,K - 1)dp[i][o] = inf;
}
ff(i,mid * K,min((mid + 1) * K, n) - 1){
ff(o,0,K - 1)dp[i][o] = inf;
dp[i][i % K] = 0;
}
// desno
ff(i,mid + 1,r){
int L = i * K;
int R = min((i + 1) * K, n) - 1;
ff(j,L,R){
for(auto c : rg[j]){
ff(o,0,K - 1){
dp[j][o] = min(dp[j][o], dp[c.fi][o] + c.se);
}
}
}
}
// levo
fb(i,mid - 1,l){
int L = i * K;
int R = min((i + 1) * K, n) - 1;
ff(j,L,R){
for(auto c : g[j]){
ff(o,0,K - 1){
dp[j][o] = min(dp[j][o], dp[c.fi][o] + c.se);
}
}
}
}
vector<array<int,5>> todo[2];
for(auto c : upit){
int L = c[0];
int R = c[1];
int a = c[2];
int b = c[3];
int i = c[4];
if(R < mid || L > mid){
todo[L > mid].pb(c);
}
else{
// mogu da odgovorim, sta vise moram
res[i] = inf;
ff(o,0,K - 1){
if(dp[a][o] != inf && dp[b][o] != inf){
res[i] = min(res[i], dp[a][o] + dp[b][o]);
}
}
if(res[i] == inf)res[i] = -1;
}
}
divi(l, mid - 1, todo[0]); divi(mid + 1, r, todo[1]);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> K >> n >> m >> q;
map<pii,int> ima;
ff(i,1,m){
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
rg[v].pb({u, w});
ima[{u, v}] = w;
}
vector<array<int,5>> upit;
ff(i,1,q){
int a, b;
cin >> a >> b;
int A = a / K, B = b / K;
if(A == B)res[i] = -1;
else if(B == A + 1)res[i] = (ima.count({a, b}) == 1 ? ima[{a, b}] : -1);
else upit.pb({a / K, b / K, a, b, i});
}
divi(0, (n - 1) / K, upit);
ff(i,1,q)cout << res[i] << '\n';
return 0;
}
/*
5 14 5 5
0 5 9
5 12 10
0 7 7
7 12 8
4 7 10
0 12
0 5
0 7
7 12
0 13
// probati bojenje sahovski
*/
# | 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... |