#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int k,m,no[505];
int ex, ey;
int vx[505][101], vy[505][101];
long long dp[505][101];
long long dist(int x, int y, int xx, int yy){
int dx = abs(xx-x);
int dy = abs(yy-y);
if(m == 1) return dx + dy;
dx = 2 * dx / (m+1) + dx % (m+1);
dy = 2 * dy / (m+1) + dy % (m+1);
return dx + dy + 2;
}
long long f(int pos, int ob){
if(pos == k) return dist(vx[pos][ob],vy[pos][ob],ex,ey);
if(dp[pos][ob]) return dp[pos][ob];
long long res = 1e13;
for (int i=0; i<no[pos+1]; i++) {
res = min(res,dist(vx[pos][ob],vy[pos][ob],vx[pos+1][i],vy[pos+1][i]) + f(pos+1,i));
}
return dp[pos][ob] = res;
}
int main(){
scanf("%d %d",&k,&m);
scanf("%d %d %d %d",&ex,&ey,&vx[1][0],&vy[1][1]);
for (int i=k; i>=2; i--) {
scanf("%d",&no[i]);
for (int j=0; j<no[i]; j++) {
scanf("%d %d",&vx[i][j],&vy[i][j]);
}
}
long long res = f(1,0);
if(res >= 1e13 - 1) printf("-1");
else printf("%lld",res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1884 KB |
Output is correct |
2 |
Correct |
0 ms |
1884 KB |
Output is correct |
3 |
Correct |
0 ms |
1884 KB |
Output is correct |
4 |
Incorrect |
0 ms |
1884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |