This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
int32_t main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int K, n, m, q;
cin >> K >> n >> m >> q;
auto node = [&](int i, int j) { return K * i + j; };
auto id = [&](int x) { return pii{ x / K, x % K }; };
ll dp[n/K+1][K+1][n/K+1][K+1];
for(int i=0; i<=n/K; i++)
for(int j=0; j<=K; j++)
for(int k=0; k<=n/K; k++)
for(int l=0; l<=K; l++) dp[i][j][k][l] = 1e9;
for(int i=0; i<=n/K; i++)
for(int j=0; j<=K; j++) dp[i][j][i][j] = 0;
for(int i=0; i<m; i++) {
int a, b, t;
cin >> a >> b >> t;
dp[a/K][a%K][b/K][b%K] = min(dp[a/K][a%K][b/K][b%K], (ll)t);
}
//compute dp in increasing dist
for(int d=2; d<=n/K; d++) {
for(int i=0; i+d<=n/K; i++) {
for(int j=0; j<K; j++) {
for(int k=0; k<K; k++) {
if(node(i+d, k) > n) break;
for(int a=i+1; a<i+d; a++) {
for(int b=0; b<K; b++) {
dp[i][j][i+d][k] = min(dp[i][j][i+d][k], dp[i][j][a][b] + dp[a][b][i+d][k]);
}
}
}
}
}
}
while(q--) {
int a, b;
cin >> a >> b;
cout << (dp[a/K][a%K][b/K][b%K] == 1e9 ? -1 : dp[a/K][a%K][b/K][b%K]) << '\n';
}
return 0;
}
Compilation message (stderr)
toll.cpp: In function 'int32_t main()':
toll.cpp:29:10: warning: variable 'id' set but not used [-Wunused-but-set-variable]
29 | auto id = [&](int x) { return pii{ x / K, x % K }; };
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |