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 int long long
using namespace std;
/*
3 1
5 2
1 6
1 5 5
1 3 2
*/
const int nmax = 696;
int sz[nmax];
struct str {
int i, j;
} mat[nmax][nmax];
int dp[nmax][nmax];
int32_t main() {
for(int i = 0; i < nmax; i ++) {
for(int j = 0; j < nmax; j ++) {
dp[i][j] = 1e9;
}
}
int n, useless, si, sj, ei, ej;
cin >> n >> useless >> si >> sj >> ei >> ej;
if(n == 1) {
cout << abs(si - ei) + abs(sj - ej);
return 0;
}
for(int i = n; i > 1; i --) {
int x;
cin >> x;
sz[i] = x;
for(int j = 1; j <= x; j ++) {
cin >> mat[i][j].i >> mat[i][j].j;
}
}
for(int i = 1; i <= sz[n]; i ++) {
dp[n][i] = abs(si - mat[n][i].i) + abs(sj - mat[n][i].j);
}
for(int et = n - 1; et > 1; et --) {
for(int sus = 1; sus <= sz[et + 1]; sus ++) {
for(int jos = 1; jos <= sz[et]; jos ++) {
int x = dp[et + 1][sus] + abs(mat[et + 1][sus].i - mat[et][jos].i) + abs(mat[et + 1][sus].j - mat[et][jos].j);
dp[et][jos] = min(dp[et][jos], x);
}
}
}
int ans = 1e9;
for(int ult = 1; ult <= sz[2]; ult ++) {
int x = dp[2][ult] + abs(mat[2][ult].i - ei) + abs(mat[2][ult].j - ej);
ans = min(ans, x);
}
cout << ans;
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... |