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>
using namespace std;
const int inf = 1e8 + 10;
const int mxn = 330;
int a[mxn], n, m;
int b[mxn], k, q;
int dp[mxn][mxn];
int main() {
cin >> n >> k;
for(int i = 0; i < mxn; i++)
for(int j = 0; j < mxn; j++)
dp[i][j] = 1e8 + 10;
cin >> m;
for(int i = 0; i < m; i++) {
cin >> a[i] >> b[i];
if(a[i] < b[i])
for(int t = 0; t < k; t++) {
int st = a[i] + t;
if(st > b[i]) break;
for(int en = st; en <= b[i]; en++)
dp[st][en] = 1;
}
else
for(int t = 0; t < k; t++) {
int st = a[i] - t;
if(st < b[i]) break;
for(int en = st; en >= b[i]; en--)
dp[st][en] = 1;
}
}
for(int mid = 0; mid < mxn; mid++)
for(int st = 0; st < mxn; st++)
for(int en = 0; en < mxn; en++)
dp[st][en] = min(dp[st][en], dp[st][mid] + dp[mid][en]);
int q;
cin >> q;
while(q--) {
int st, en;
cin >> st >> en;
if(dp[st][en] >= inf) cout << -1 << '\n';
else cout << dp[st][en] << '\n';
}
return 0;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |