#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];
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){
dp[i] = inf;
}
ff(i,mid * K,min((mid + 1) * K, n) - 1){
dp[i] = 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]){
dp[j] = min(dp[j], dp[c.fi] + 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]){
dp[j] = min(dp[j], dp[c.fi] + 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] = (dp[a] == inf || dp[b] == inf ? -1 : dp[a] + dp[b]);
}
}
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
16768 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9940 KB |
Output is correct |
8 |
Correct |
52 ms |
16764 KB |
Output is correct |
9 |
Correct |
52 ms |
16564 KB |
Output is correct |
10 |
Correct |
8 ms |
10640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
99 ms |
19768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Incorrect |
5 ms |
9684 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
16768 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9812 KB |
Output is correct |
6 |
Correct |
5 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9940 KB |
Output is correct |
8 |
Correct |
52 ms |
16764 KB |
Output is correct |
9 |
Correct |
52 ms |
16564 KB |
Output is correct |
10 |
Correct |
8 ms |
10640 KB |
Output is correct |
11 |
Incorrect |
99 ms |
19768 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |