#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int MOD = 998244353;
void solve(){
int n, o, k, m; cin >> k >> n >> m >> o;
vector<vector<pair<int, int>>> adj(n);
for(int i = 0; i < m; i++){
int a, b, t; cin >> a >> b >> t;
adj[a].push_back({b, t});
}
int groups = (n - 1) / k + 1;
int sqr = sqrt(groups);
vector<vector<int>> calc(n, vector<int> (k, 1e17));
for(int i = 0; i < groups; i += sqr){
for(int j = i * k; j < (i + 1) * k && j < n; j++){
queue<int> q;
q.push(j);
map<int, int> d; d[j] = 0;
while(!q.empty()){
int x = q.front(); q.pop();
if(x / k == i + sqr){
calc[j][x % k] = d[x];
continue;
}
if((x / k) % sqr != 0){
calc[x][j % k] = d[x];
}
for(auto p : adj[x]){
int node = p.first;
bool gd = d.count(node);
if(!gd) {d[node] = d[x] + p.second; q.push(node);}
else d[node] = min(d[node], d[x] + p.second);
}
}
}
}
while(o--){
int a, b; cin >> a >> b;
if(a / k >= b / k){
cout << -1 << endl;
continue;
}
queue<int> q;
q.push(a);
map<int, int> d; d[a] = 0;
vector<int> dist(k, 1e17);
int gr = -1;
bool done = 0;
while(!q.empty()){
int x = q.front(); q.pop();
if(done) continue;
if((x / k) % sqr == 0){
gr = x / k;
dist[x % k] = d[x];
continue;
}
if(x == b){
cout << d[x] << endl;
done = 1;
continue;
}
for(auto p : adj[x]){
int node = p.first;
bool gd = d.count(node);
if(!gd) {d[node] = d[x] + p.second; q.push(node);}
else d[node] = min(d[node], d[x] + p.second);
}
}
if(done) continue;
if(gr == -1){
cout << -1 << endl;
continue;
}
while((gr + sqr) * k <= b){
vector<int> nw(k, 1e17);
for(int i = 0; i < k && gr * k + i < n; i++){
for(int j = 0; j < k; j++){
nw[j] = min(nw[j], dist[i] + calc[gr * k + i][j]);
}
}
dist = nw;
gr += sqr;
}
if((b / k) % sqr == 0) cout << (dist[b % k] >= 1e10 ? -1 : dist[b % k]) << endl;
else{
int ans = 1e17;
for(int i = 0; i < k; i++){
ans = min(ans, dist[i] + calc[b][i]);
}
if(ans >= 1e10) cout << -1 << endl;
else cout << ans << endl;
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("inpt.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
//int tt; cin >> tt;
int tt = 1;
for(int t = 1; t <= tt; t++){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
5956 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
128 ms |
6656 KB |
Output is correct |
9 |
Correct |
35 ms |
6508 KB |
Output is correct |
10 |
Correct |
7 ms |
4376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
6648 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
316 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
7 ms |
468 KB |
Output is correct |
8 |
Correct |
8 ms |
588 KB |
Output is correct |
9 |
Correct |
49 ms |
6612 KB |
Output is correct |
10 |
Correct |
134 ms |
10120 KB |
Output is correct |
11 |
Correct |
87 ms |
8356 KB |
Output is correct |
12 |
Correct |
62 ms |
7500 KB |
Output is correct |
13 |
Correct |
146 ms |
9924 KB |
Output is correct |
14 |
Correct |
67 ms |
6608 KB |
Output is correct |
15 |
Correct |
83 ms |
6348 KB |
Output is correct |
16 |
Correct |
83 ms |
6200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
3 ms |
544 KB |
Output is correct |
10 |
Correct |
25 ms |
6560 KB |
Output is correct |
11 |
Correct |
56 ms |
8032 KB |
Output is correct |
12 |
Correct |
98 ms |
9932 KB |
Output is correct |
13 |
Correct |
108 ms |
10700 KB |
Output is correct |
14 |
Correct |
87 ms |
8988 KB |
Output is correct |
15 |
Correct |
79 ms |
6220 KB |
Output is correct |
16 |
Correct |
78 ms |
6220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
5 ms |
588 KB |
Output is correct |
9 |
Correct |
3 ms |
544 KB |
Output is correct |
10 |
Correct |
25 ms |
6560 KB |
Output is correct |
11 |
Correct |
56 ms |
8032 KB |
Output is correct |
12 |
Correct |
98 ms |
9932 KB |
Output is correct |
13 |
Correct |
108 ms |
10700 KB |
Output is correct |
14 |
Correct |
87 ms |
8988 KB |
Output is correct |
15 |
Correct |
79 ms |
6220 KB |
Output is correct |
16 |
Correct |
78 ms |
6220 KB |
Output is correct |
17 |
Correct |
147 ms |
8248 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
324 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
5 ms |
480 KB |
Output is correct |
24 |
Incorrect |
11 ms |
500 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
5956 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
128 ms |
6656 KB |
Output is correct |
9 |
Correct |
35 ms |
6508 KB |
Output is correct |
10 |
Correct |
7 ms |
4376 KB |
Output is correct |
11 |
Correct |
79 ms |
6648 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
316 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
7 ms |
468 KB |
Output is correct |
18 |
Correct |
8 ms |
588 KB |
Output is correct |
19 |
Correct |
49 ms |
6612 KB |
Output is correct |
20 |
Correct |
134 ms |
10120 KB |
Output is correct |
21 |
Correct |
87 ms |
8356 KB |
Output is correct |
22 |
Correct |
62 ms |
7500 KB |
Output is correct |
23 |
Correct |
146 ms |
9924 KB |
Output is correct |
24 |
Correct |
67 ms |
6608 KB |
Output is correct |
25 |
Correct |
83 ms |
6348 KB |
Output is correct |
26 |
Correct |
83 ms |
6200 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
324 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
5 ms |
588 KB |
Output is correct |
35 |
Correct |
3 ms |
544 KB |
Output is correct |
36 |
Correct |
25 ms |
6560 KB |
Output is correct |
37 |
Correct |
56 ms |
8032 KB |
Output is correct |
38 |
Correct |
98 ms |
9932 KB |
Output is correct |
39 |
Correct |
108 ms |
10700 KB |
Output is correct |
40 |
Correct |
87 ms |
8988 KB |
Output is correct |
41 |
Correct |
79 ms |
6220 KB |
Output is correct |
42 |
Correct |
78 ms |
6220 KB |
Output is correct |
43 |
Correct |
147 ms |
8248 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
324 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
5 ms |
480 KB |
Output is correct |
50 |
Incorrect |
11 ms |
500 KB |
Output isn't correct |
51 |
Halted |
0 ms |
0 KB |
- |