#include <bits/stdc++.h>
using namespace std;
#define int long long int
vector<vector<int>> calc(vector<vector<int>> a, vector<vector<int>> b, int k) {
vector<vector<int>> ans(k, vector<int>(k, INT_MAX));
for (int x = 0; x < k; x++) {
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
ans[i][j] = min(ans[i][j], a[i][x]+b[x][j]);
}
}
}
return ans;
}
void solve() { // I dont really like this problem
int n, o, m, k;
cin >> k >> n >> m >> o;
vector<vector<vector<vector<int>>>> dp(n+1, vector<vector<vector<int>>>(17, vector<vector<int>>(k, vector<int>(k, INT_MAX))));
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
dp[u/k][0][u%k][v%k] = dp[u/k][0][u%k][v%k];
}
for (int j = 1; j < 17; j++) {
for (int i = 0; i < (n+k-1)/k; i++) {
if (i+(1<<j) >= (n+k-1)/k) {
break;
}
dp[i][j] = calc(dp[i][j-1], dp[i+(1<<j-1)][j-1], k);
}
}
while(o--) {
int a, b;
cin >> a >> b;
vector<vector<int>> ans(k, vector<int>(k, INT_MAX));
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
ans[i][i] = 0;
}
}
int i = a/k;
for (int j = 16; j >= 0; j--) {
if (i+(1<<j) <= b/k) {
ans = calc(ans, dp[i][j], k);
i += (1 << j);
}
}
if (ans[a%k][b%k] == INT_MAX) {
cout << -1 << endl;
} else {
cout << ans[a%k][b%k] << endl;
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
t = 1;
while(t--) {
solve();
}
}
Compilation message
toll.cpp: In function 'void solve()':
toll.cpp:29:51: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
29 | dp[i][j] = calc(dp[i][j-1], dp[i+(1<<j-1)][j-1], k);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
230 ms |
75196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
201 ms |
128344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
230 ms |
75196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |