結果

問題 No.2695 Warp Zone
ユーザー mayocornmayocorn
提出日時 2024-03-22 22:04:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 4,919 bytes
コンパイル時間 5,065 ms
コンパイル使用メモリ 281,544 KB
実行使用メモリ 35,840 KB
最終ジャッジ日時 2024-09-30 11:35:07
合計ジャッジ時間 7,773 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 195 ms
35,840 KB
testcase_04 AC 185 ms
35,200 KB
testcase_05 AC 186 ms
35,200 KB
testcase_06 AC 193 ms
35,712 KB
testcase_07 AC 196 ms
35,840 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 61 ms
19,968 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 17 ms
8,320 KB
testcase_12 AC 90 ms
24,960 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 116 ms
28,544 KB
testcase_15 AC 53 ms
18,688 KB
testcase_16 AC 13 ms
7,680 KB
testcase_17 AC 28 ms
9,984 KB
testcase_18 AC 147 ms
31,616 KB
testcase_19 AC 2 ms
5,248 KB
testcase_20 AC 128 ms
30,080 KB
testcase_21 AC 116 ms
28,416 KB
testcase_22 AC 41 ms
11,520 KB
testcase_23 AC 88 ms
24,320 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 2 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0