# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
207546 | MetB | OBELISK (NOI14_obelisk) | C++14 | 0 ms | 0 KiB |
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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define N 2000001
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
int qdx[3] = {-1, 1, 0};
struct Pt {
ll x, y;
} S, E;
int n, m;
vector <Pt> p[N];
vector <ll> d[N];
ll dist (Pt a, Pt b) {
ll x = abs (a.x - b.x), y = abs (a.y - b.y);
if(m == 1)
return abs(x) + abs(y);
int ans = (1<<30);
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
for(int g = 0; g < 2; g++)
{
for(int h = 0; h < 2; h++)
{
int p = abs(x + dx[i] * (m + 1));
int q = abs(y + dx[j] * (m + 1));
int t1 = p / (m + 1) + (i < 2);
int t2 = q / (m + 1) + (j < 2);
int cur = t1 * 2 + t2 * 2 + g * 2 + h * 2;
p %= (m + 1);
q %= (m + 1);
if(p)
{
if(!t2)
cur += 2;
if(g)
cur += m + 1 - p;
else
cur += p;
}
if(q)
{
if(!t1)
cur += 2;
if(h)
cur += m + 1 - q;
else
cur += q;
}
ans = min(ans, cur);
}
}
}
}
return ans;
}
int main () {
cin >> n >> m;
cin >> S.x >> S.y >> E.x >> E.y;
d[0].push_back (0);
p[0].push_back (S);
for (int i = 1; i < n; i++) {
int h;
cin >> h;
d[i].resize (h);
p[i].resize (h);
for (int j = 0; j < h; j++) {
cin >> p[i][j].x >> p[i][j].y;
d[i][j] = INF;
for (int k = 0; k < p[i-1].size (); k++) {
d[i][j] = min (d[i][j], d[i-1][k] + dist (p[i-1][k], p[i][j]));
}
}
}
ll ans = INF;
for (int i = 0; i < p[n-1].size (); i++) {
ans = min (ans, d[n-1][i] + dist (p[n-1][i], E));
}
cout << (ans == INF ? -1 : ans);
}