//Be Name Khoda
// 17:00 | 15:40
#include<bits/stdc++.h>
#pragma GCC optimize ("unroll-loops")
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(x) x.begin(), x.end()
#define pll pair<ll, ll>
#define pii pair<int, int>
#define plll pair<pll, ll>
#define piii pair<pii, int>
#define piiii pair<pii, pii>
const int mxn = 5e4 + 5;
int n, m, k, o;
int dis[mxn][18][5], arr[5][2];
vector<pii> adj[mxn], Qu;
void input() {
for(int i = 0; i < mxn; i++) {
for(int j = 0; j < 18; j++) {
for(int e = 0; e < 5; e++) {
dis[i][j][e] = 1e9;
}
}
}
cin >> k >> n >> m >> o;
int v, u, w;
for(int i = 0; i < m; i++) {
cin >> v >> u >> w;
adj[v].push_back({u, w});
dis[v][0][u % k] = w;
}
for(int i = 0; i < o; i++) {
cin >> v >> u;
if(v / k >= u / k) {
Qu.push_back({-1, -1});
continue;
}
Qu.push_back({v, u});
}
}
void solve() {
for(int i = 1; i < 18; i++) {
for(int j = 0; j < n; j++) {
for(int w = 0; w < k; w++) {
for(int q = 0; q < k; q++) {
int t = j + (k * (1 << (i - 1))) - (j % k) + q;
if(t >= n) {
break;
}
dis[j][i][w] = min(dis[j][i][w], dis[j][i - 1][q] + dis[t][i - 1][w]);
}
}
}
}
int x, gn;
vector<int> v;
for(auto Q : Qu) {
if(Q.first == -1) {
cout << "-1" << endl;
continue;
}
for(int i = 0; i < k; i++) {
arr[i][0] = 1e9;
arr[i][1] = 1e9;
}
v.push_back(Q.first);
arr[Q.first % k][0] = 0;
gn = Q.first / k;
x = (Q.second / k) - (Q.first / k);
for(int i = 17; i > -1; i--) {
if(x - (1 << i) >= 0) {
x -= (1 << i);
gn += (1 << i);
for(int j = 0; j < k; j++) {
for(auto g : v) {
arr[j][1] = min(arr[j][1], arr[g % k][0] + dis[g][i][j]);
}
}
v.clear();
for(int j = 0; j < k; j++) {
arr[j][0] = arr[j][1];
arr[j][1] = 1e9;
if(arr[j][0] < 1e9) {
v.push_back(k * gn + j);
}
}
if(int(v.size()) == 0) {
cout << "-1" << endl;
continue;
}
}
}
if(arr[Q.second % k][0] >= 1e9) {
arr[Q.second % k][0] = -1;
}
cout << arr[Q.second % k][0] << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
input(), solve();
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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
92 ms |
20968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
178 ms |
20996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
19020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
19020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
92 ms |
20968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |