#include <bits/stdc++.h>
//#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 5e4 + 5;
int K, N, M, Q;
vector<ll> ans, dist(maxn);
vector<pii> g1[maxn], g2[maxn];
void D(int s, int l, int r) {
for(int i=0; i<N; i++) dist[i] = 1e18;
dist[s] = 0;
priority_queue<pll, vector<pll>, greater<pll> > pq;
pq.push({ 0, s });
while(!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if(d != dist[u]) continue;
for(auto &[v, w] : g1[u]) {
if(v / K > r || v / K < l) continue;
if(dist[v] > dist[u] + w) {
dist[v] = d + w;
pq.push({ d + w, v });
}
}
}
pq.push({ 0, s });
while(!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if(d != dist[u]) continue;
for(auto &[v, w] : g2[u]) {
if(v / K < l || v / K > r) continue;
if(dist[v] > dist[u] + w) {
dist[v] = d + w;
pq.push({ d + w, v });
}
}
}
}
void f(int l, int r, vector<array<int, 3> > &qus) {
if(l > r) return ;
int tm = (l + r) / 2;
vector<array<int, 3> > L, R;
for(int i=tm*K; i<min(N, tm * K + K); i++) {
D(i, l, r);
for(auto &[a, b, id] : qus) ans[id] = min(ans[id], dist[a] + dist[b]);
}
for(auto i=0; i<qus.size(); i++) {
if(qus[i][1] / K < tm) L.push_back(qus[i]);
if(qus[i][0] / K > tm) R.push_back(qus[i]);
}
f(l, tm-1, L);
f(tm+1, r, R);
}
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
cin >> K >> N >> M >> Q;
ans.resize(Q, 1e9);
vector<array<int, 3> > qus;
for(int i=0; i<M; i++) {
int a, b, t;
cin >> a >> b >> t;
g1[a].push_back({ b, t });
g2[b].push_back({ a, t });
}
for(int i=0; i<Q; i++) {
int a, b;
cin >> a >> b;
if(a / K == b / K) ans[i] = -1;
else qus.push_back({ a, b, i });
}
f(0, (N - 1) / K, qus);
for(int x : ans) cout << (x == 1e9 ? -1 : x) << '\n';
return 0;
}
Compilation message
toll.cpp: In function 'void f(int, int, std::vector<std::array<int, 3> >&)':
toll.cpp:63:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(auto i=0; i<qus.size(); i++) {
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
891 ms |
6664 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3160 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
2 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3160 KB |
Output is correct |
8 |
Correct |
879 ms |
6664 KB |
Output is correct |
9 |
Correct |
929 ms |
6488 KB |
Output is correct |
10 |
Correct |
848 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
933 ms |
6676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3164 KB |
Output is correct |
8 |
Incorrect |
5 ms |
3420 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3164 KB |
Output is correct |
8 |
Incorrect |
5 ms |
3420 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
891 ms |
6664 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3160 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
2 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3160 KB |
Output is correct |
8 |
Correct |
879 ms |
6664 KB |
Output is correct |
9 |
Correct |
929 ms |
6488 KB |
Output is correct |
10 |
Correct |
848 ms |
3420 KB |
Output is correct |
11 |
Incorrect |
933 ms |
6676 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |