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;
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
using db = long double;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
tcT> bool ckmin(T&a, const T&b) {
return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
return b > a ? a=b,1 : 0; }
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int MX = 505;
int K, M;
V<pi> lst[MX];
int sx, sy, ex, ey;
int inds[MX];
V<pi> adj[MX*MX];
int dist1D(int delta) {
if(delta == 0) return 0;
if(delta%(M+1) == 0) return 2*(delta/(M+1));
int fldiv = delta/(M+1);
int opt1 = fldiv*2 + delta%(M+1) + 2;
int opt2 = (fldiv+1)*2 + M+1 - (delta%(M+1)) + 2;
return min(opt1, opt2);
}
bool canSaveBoth(int dx, int dy) {
if(dx >= M+1 && dy >= M+1 && dx%(M+1) && dy%(M+1)) return true;
return false;
}
bool ok(int delta) {
return delta%(M+1) != 0 && delta >= M+1;
}
int dist2D(int dx, int dy) {
int distx = dist1D(dx);
int disty = dist1D(dy);
int ans = distx + disty;
if(canSaveBoth(dx, dy)) ans -= 4;
else if(ok(dx) || ok(dy)) ans -= 2;
return ans;
}
ll dist[MX];
void dijkstra(int src) {
memset(dist, 0x3f, sizeof dist);
pqg<pair<ll,int>> pq;
pq.push({dist[src] = 0, src});
while(sz(pq)) {
ll du = pq.top().fi;
int u = pq.top().se;
pq.pop();
if(du > dist[u]) continue;
each(ed, adj[u]) {
int v = ed.fi;
ll dv = dist[u] + ed.se;
if(ckmin(dist[v], dv)) {
pq.push({dv, v});
}
}
}
}
void solve() {
cin >> K >> M;
cin >> sx >> sy >> ex >> ey;
lst[0] = {{ex, ey}};
lst[K] = {{sx, sy}};
ROF(i,1,K) {
int num;
cin >> num;
lst[i].resize(num);
F0R(j, num) {
cin >> lst[i][j].fi >> lst[i][j].se;
}
}
F0R(i, K) inds[i+1] = inds[i] + sz(lst[i]);
FOR(fl,1,K+1) {
F0R(i, sz(lst[fl]))
F0R(j, sz(lst[fl-1])) {
int dx = abs(lst[fl][i].fi - lst[fl-1][j].fi), dy = abs(lst[fl][i].se - lst[fl-1][j].se);
// dbg(dx, dy, dist2D(dx, dy));
adj[inds[fl]+i].pb({inds[fl-1]+j, dist2D(abs(lst[fl][i].fi - lst[fl-1][j].fi), abs(lst[fl][i].se - lst[fl-1][j].se))});
}
}
// dbg(dist1D(1));
dijkstra(inds[K]);
cout << dist[0] << nl;
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message (stderr)
obelisk.cpp: In function 'void solve()':
obelisk.cpp:129:21: warning: unused variable 'dx' [-Wunused-variable]
129 | int dx = abs(lst[fl][i].fi - lst[fl-1][j].fi), dy = abs(lst[fl][i].se - lst[fl-1][j].se);
| ^~
obelisk.cpp:129:64: warning: unused variable 'dy' [-Wunused-variable]
129 | int dx = abs(lst[fl][i].fi - lst[fl-1][j].fi), dy = abs(lst[fl][i].se - lst[fl-1][j].se);
| ^~
obelisk.cpp: In function 'void setIO(std::string)':
obelisk.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
obelisk.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |