結果
| 問題 |
No.2695 Warp Zone
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-03-22 22:04:03 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 201 ms / 2,000 ms |
| コード長 | 4,919 bytes |
| コンパイル時間 | 4,974 ms |
| コンパイル使用メモリ | 271,980 KB |
| 最終ジャッジ日時 | 2025-02-20 11:42:16 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
// type
typedef long long ll;
typedef long double ld;
using i128 = __int128_t;
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template<class T> using v = vector<T>;
#define V vector
#define pl pair<ll, ll>
#define vl v<ll>
#define vp v<pl>
#define vm v<mint>
// IN-OUT
#define NYAN ios::sync_with_stdio(false);cin.tie(nullptr);cout<<fixed<<setprecision(15);
ostream &operator<<(ostream &os, const i128 &v) {
if(v == 0) { return (os << "0"); }
i128 num = v;
if(v < 0) { os << '-'; num = -num; }
string s;
for(; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }
reverse(s.begin(), s.end());
return (os << s);
}
void Yes(bool b=1) { cout << ( b == 1 ? "Yes" : "No" ) << "\n"; }
void YES(bool b=1) { cout << ( b == 1 ? "YES" : "NO" ) << "\n"; }
void No(bool b=1) { cout << ( b == 1 ? "No" : "Yes" ) << "\n"; }
void NO(bool b=1) { cout << ( b == 1 ? "NO" : "YES" ) << "\n"; }
void CIN() {}
template <typename T, class... U> void CIN(T &t, U &...u) { cin >> t; CIN(u...); }
void COUT() { cout << "\n"; }
template <typename T, class... U, char sep = ' '> void COUT(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; COUT(u...); }
#define dump(x) cerr << #x << ":"<< x << "\n";
#define vdump(x) rep(repeat, sz(x)) cerr << repeat << ":" << x[repeat] << "\n";
// macro
#define bp __builtin_popcountll
#define ALL(x) x.begin(),x.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define reps(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define sz(x) (ll)x.size()
ll xd[]={0, 1, 0, -1, 1, 1, -1, -1};
ll yd[]={1, 0, -1, 0, 1, -1, -1, 1};
// function
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T>int bl(T x) { int s = 0; while(x) {x >>= 1; s++;} return s; } // bitlength
ll rmax(ll a, ll b){return max(a, b);}
ll rmin(ll a, ll b){return min(a, b);}
ll rsum(ll a, ll b){return a + b;}
ll rzero(){return 0;}
// constant
long double PI = 3.14159265358979;
#define INF32 2147483647
#define INF64 9223372036854775807
#define INF 922337203685477580
using mint = modint998244353;
// using mint = modint1000000007;
/* SOLVE BEGIN ************************************************************************** */
// Compress(座標圧縮) ------------------------------
template< typename T = long long >
struct Compress {
vector< T > xs;
Compress() = default;
Compress(const vector< T > &vs) { add(vs); }
Compress(const initializer_list< vector< T > > &vs) { for(auto &p : vs) add(p); }
void add(const vector< T > &vs) { copy(begin(vs), end(vs), back_inserter(xs)); }
void add(const T &x) { xs.emplace_back(x); }
void init() {
sort(begin(xs), end(xs));
xs.erase(unique(begin(xs), end(xs)), end(xs));
}
vector< int > get(const vector< T > &vs) const {
vector< int > ret;
transform(begin(vs), end(vs), back_inserter(ret), [&](const T &x) {
return lower_bound(begin(xs), end(xs), x) - begin(xs);
});
return ret;
}
int get(const T &x) const { return lower_bound(begin(xs), end(xs), x) - begin(xs); }
const T &operator[](int k) const { return xs[k]; }
int size(){return xs.size();}
};
// Compress(座標圧縮) ------------------------------
void solve()
{
ll h, w, n;
cin >> h >> w >> n;
vl sx(n), sy(n), gx(n), gy(n);
rep(i, n){
CIN(sx[i], sy[i], gx[i], gy[i]);
sx[i]--; sy[i]--; gx[i]--; gy[i]--;
}
set<ll> s;
rep(i, n){
s.emplace(sx[i] * w + sy[i]);
s.emplace(gx[i] * w + gy[i]);
}
s.emplace(0);
s.emplace(h * w - 1);
Compress<ll> X;
for(auto x : s) X.add(x);
X.init();
ll N = X.size();
v<vp> edge(N);
ll st = X.get(0), g = X.get(h * w - 1);
rep(i, n){
ll id = X.get(gx[i] * w + gy[i]);
edge[id].emplace_back(g, abs(gx[i] - (h - 1)) + abs(gy[i] - (w - 1)));
}
rep(i, n){
ll si = X.get(sx[i] * w + sy[i]);
ll gi = X.get(gx[i] * w + gy[i]);
edge[si].emplace_back(gi, 1);
rep(j, n){
ll id = X.get(gx[j] * w + gy[j]);
edge[id].emplace_back(si, abs(sx[i] - gx[j]) + abs(sy[i] - gy[j]));
id = X.get(sx[j] * w + sy[j]);
edge[id].emplace_back(si, abs(sx[i] - sx[j]) + abs(sy[i] - sy[j]));
}
}
rep(i, n) edge[st].emplace_back(X.get(sx[i] * w + sy[i]), sx[i] + sy[i]);
vl dist(N, INF), visit(N);
dist[X.get(0)] = 0;
pqg<pl> Q;
Q.emplace(0, X.get(0));
while(sz(Q)){
auto [d, id] = Q.top(); Q.pop();
if(visit[id]) continue;
visit[id] = 1;
for(auto [next, c] : edge[id]){
if(chmin(dist[next], dist[id] + c)) Q.emplace(dist[next], next);
}
}
cout << min(dist[g], h + w - 2) << "\n";
}
int main()
{
NYAN
solve();
return 0;
}