#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, char separator = ' ', char finish = '\n'){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const long N = 5e4 + 69, INF = 1e9 + 69;
long k, n, m, o;
vector<pair<long, long>> graph[N], rgraph[N];
long ans[N];
pair<long, long> queries[N];
long dp[N];
void dfs(long l, long r, vector<long> cur){
if (l >= r) return;
long mid = (l + r) >> 1;
vector<long> L, R, pending;
for(long i: cur){
if (queries[i].second / k < mid) L.push_back(i);
else if (queries[i].first / k > mid) R.push_back(i);
else pending.push_back(i);
}
if (L.size()) dfs(l, mid - 1, L);
if (R.size()) dfs(mid + 1, r, R);
if (pending.empty()) return;
for(long i = mid * k; i < (mid + 1) * k; ++i) {
for(long j = mid * k; j < (mid + 1) * k; ++j) dp[j] = INF;
dp[i] = 0;
for(long i = mid * k - 1; i >= l * k; --i) {
dp[i] = INF;
for(auto j: graph[i]) minimize(dp[i], dp[j.first] + j.second);
}
for(long i = (mid+1) * k; i < (r+1) * k; ++i) {
dp[i] = INF;
for(auto j: rgraph[i]) minimize(dp[i], dp[j.first] + j.second);
}
for(long i: pending){
minimize(ans[i], dp[queries[i].first] + dp[queries[i].second]);
}
}
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> k >> n >> m >> o;
while(m--){
long u, v, w; cin >> u >> v >> w;
graph[u].push_back({v, w});
rgraph[v].push_back({u, w});
}
vector<long> cur(o);
for(long i = 0; i<o; ++i) {
cin >> queries[i].first >> queries[i].second;
cur[i] = i;
ans[i] = INF;
}
dfs(0, (n + k - 1) / k, cur);
for(long i = 0; i<o; ++i) if (ans[i] == INF) cout << -1 << "\n";
else cout << ans[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4220 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
19 ms |
8540 KB |
Output is correct |
9 |
Correct |
17 ms |
8568 KB |
Output is correct |
10 |
Correct |
4 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
11288 KB |
Output is correct |
2 |
Correct |
1 ms |
4184 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4168 KB |
Output is correct |
5 |
Correct |
1 ms |
4188 KB |
Output is correct |
6 |
Correct |
1 ms |
4212 KB |
Output is correct |
7 |
Correct |
4 ms |
4696 KB |
Output is correct |
8 |
Correct |
4 ms |
4700 KB |
Output is correct |
9 |
Correct |
18 ms |
8796 KB |
Output is correct |
10 |
Correct |
66 ms |
14772 KB |
Output is correct |
11 |
Correct |
33 ms |
11604 KB |
Output is correct |
12 |
Correct |
26 ms |
10064 KB |
Output is correct |
13 |
Correct |
59 ms |
16248 KB |
Output is correct |
14 |
Correct |
31 ms |
11136 KB |
Output is correct |
15 |
Correct |
29 ms |
9552 KB |
Output is correct |
16 |
Correct |
28 ms |
9496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4204 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
3 ms |
4440 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
15 ms |
8284 KB |
Output is correct |
11 |
Correct |
49 ms |
10836 KB |
Output is correct |
12 |
Correct |
50 ms |
14164 KB |
Output is correct |
13 |
Correct |
55 ms |
15432 KB |
Output is correct |
14 |
Correct |
52 ms |
12484 KB |
Output is correct |
15 |
Correct |
31 ms |
9564 KB |
Output is correct |
16 |
Correct |
34 ms |
9296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Correct |
1 ms |
4188 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4204 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
3 ms |
4440 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
15 ms |
8284 KB |
Output is correct |
11 |
Correct |
49 ms |
10836 KB |
Output is correct |
12 |
Correct |
50 ms |
14164 KB |
Output is correct |
13 |
Correct |
55 ms |
15432 KB |
Output is correct |
14 |
Correct |
52 ms |
12484 KB |
Output is correct |
15 |
Correct |
31 ms |
9564 KB |
Output is correct |
16 |
Correct |
34 ms |
9296 KB |
Output is correct |
17 |
Correct |
34 ms |
11088 KB |
Output is correct |
18 |
Correct |
1 ms |
4188 KB |
Output is correct |
19 |
Correct |
1 ms |
4188 KB |
Output is correct |
20 |
Correct |
1 ms |
4188 KB |
Output is correct |
21 |
Correct |
2 ms |
4188 KB |
Output is correct |
22 |
Correct |
1 ms |
4188 KB |
Output is correct |
23 |
Correct |
2 ms |
4328 KB |
Output is correct |
24 |
Correct |
3 ms |
4444 KB |
Output is correct |
25 |
Correct |
4 ms |
4444 KB |
Output is correct |
26 |
Correct |
3 ms |
4480 KB |
Output is correct |
27 |
Correct |
22 ms |
8276 KB |
Output is correct |
28 |
Correct |
55 ms |
14480 KB |
Output is correct |
29 |
Correct |
62 ms |
15708 KB |
Output is correct |
30 |
Correct |
46 ms |
12628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
8784 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4188 KB |
Output is correct |
4 |
Correct |
1 ms |
4188 KB |
Output is correct |
5 |
Correct |
2 ms |
4188 KB |
Output is correct |
6 |
Correct |
2 ms |
4220 KB |
Output is correct |
7 |
Correct |
2 ms |
4188 KB |
Output is correct |
8 |
Correct |
19 ms |
8540 KB |
Output is correct |
9 |
Correct |
17 ms |
8568 KB |
Output is correct |
10 |
Correct |
4 ms |
4696 KB |
Output is correct |
11 |
Correct |
35 ms |
11288 KB |
Output is correct |
12 |
Correct |
1 ms |
4184 KB |
Output is correct |
13 |
Correct |
2 ms |
4188 KB |
Output is correct |
14 |
Correct |
1 ms |
4168 KB |
Output is correct |
15 |
Correct |
1 ms |
4188 KB |
Output is correct |
16 |
Correct |
1 ms |
4212 KB |
Output is correct |
17 |
Correct |
4 ms |
4696 KB |
Output is correct |
18 |
Correct |
4 ms |
4700 KB |
Output is correct |
19 |
Correct |
18 ms |
8796 KB |
Output is correct |
20 |
Correct |
66 ms |
14772 KB |
Output is correct |
21 |
Correct |
33 ms |
11604 KB |
Output is correct |
22 |
Correct |
26 ms |
10064 KB |
Output is correct |
23 |
Correct |
59 ms |
16248 KB |
Output is correct |
24 |
Correct |
31 ms |
11136 KB |
Output is correct |
25 |
Correct |
29 ms |
9552 KB |
Output is correct |
26 |
Correct |
28 ms |
9496 KB |
Output is correct |
27 |
Correct |
2 ms |
4188 KB |
Output is correct |
28 |
Correct |
1 ms |
4188 KB |
Output is correct |
29 |
Correct |
1 ms |
4188 KB |
Output is correct |
30 |
Correct |
1 ms |
4188 KB |
Output is correct |
31 |
Correct |
2 ms |
4204 KB |
Output is correct |
32 |
Correct |
2 ms |
4188 KB |
Output is correct |
33 |
Correct |
2 ms |
4188 KB |
Output is correct |
34 |
Correct |
3 ms |
4440 KB |
Output is correct |
35 |
Correct |
2 ms |
4444 KB |
Output is correct |
36 |
Correct |
15 ms |
8284 KB |
Output is correct |
37 |
Correct |
49 ms |
10836 KB |
Output is correct |
38 |
Correct |
50 ms |
14164 KB |
Output is correct |
39 |
Correct |
55 ms |
15432 KB |
Output is correct |
40 |
Correct |
52 ms |
12484 KB |
Output is correct |
41 |
Correct |
31 ms |
9564 KB |
Output is correct |
42 |
Correct |
34 ms |
9296 KB |
Output is correct |
43 |
Correct |
34 ms |
11088 KB |
Output is correct |
44 |
Correct |
1 ms |
4188 KB |
Output is correct |
45 |
Correct |
1 ms |
4188 KB |
Output is correct |
46 |
Correct |
1 ms |
4188 KB |
Output is correct |
47 |
Correct |
2 ms |
4188 KB |
Output is correct |
48 |
Correct |
1 ms |
4188 KB |
Output is correct |
49 |
Correct |
2 ms |
4328 KB |
Output is correct |
50 |
Correct |
3 ms |
4444 KB |
Output is correct |
51 |
Correct |
4 ms |
4444 KB |
Output is correct |
52 |
Correct |
3 ms |
4480 KB |
Output is correct |
53 |
Correct |
22 ms |
8276 KB |
Output is correct |
54 |
Correct |
55 ms |
14480 KB |
Output is correct |
55 |
Correct |
62 ms |
15708 KB |
Output is correct |
56 |
Correct |
46 ms |
12628 KB |
Output is correct |
57 |
Correct |
91 ms |
16724 KB |
Output is correct |
58 |
Correct |
18 ms |
8536 KB |
Output is correct |
59 |
Correct |
38 ms |
11604 KB |
Output is correct |