結果

問題 No.157 2つの空洞
ユーザー raven7959raven7959
提出日時 2023-08-28 16:30:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 13,021 bytes
コンパイル時間 2,356 ms
コンパイル使用メモリ 229,972 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-09 11:42:19
合計ジャッジ時間 3,106 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2")
*/
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define rrep(i, n) for (ll i = (int)(n) - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ll(x.size())
#define yn(joken) cout<<((joken) ? "Yes" : "No")<<"\n"
#define YN(joken) cout<<((joken) ? "YES" : "NO")<<"\n"
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int,int>>;
using vpl = vector<pair<ll,ll>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vld = vector<long double>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvs = vector<vector<string>>;
using vvc = vector<vector<char>>;
using vvd = vector<vector<double>>;
using vvld = vector<vector<long double>>;
using vvvi = vector<vector<vector<int>>>;
using vvvl = vector<vector<vector<ll>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vvvvl = vector<vector<vector<vector<ll>>>>;
template <class T> using priq = priority_queue<T>;
template <class T> using priqg = priority_queue<T, vector<T>, greater<T>>;
const int INF = 1e9;
const ll LINF = 2e18;
template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }
vi iota(int n) {
    vi a(n);
    return iota(a.begin(), a.end(), 0), a;
}
template <typename T> vi iota(const vector<T> &a, bool greater = false) {
    vi ret(a.size());
    iota(ret.begin(), ret.end(), 0);
    sort(ret.begin(), ret.end(), [&](int i, int j) {
        if(greater) return a[i] > a[j];
        return a[i] < a[j];
    });
    return ret;
}
template <typename S> void rearrange(const vector<S> &id) {}
template <typename S, typename T> void rearrange_exec(const vector<S> &id, vector<T> &v) {
    vector<T> w(v.size());
    rep(i, sz(id)) w[i] = v[id[i]];
    v.swap(w);
}
// 並び替える順番, 並び替えるvector
template <typename S, typename Head, typename... Tail> void rearrange(const vector<S> &id, Head &a, Tail &...tail) {
    rearrange_exec(id, a);
    rearrange(id, tail...);
}
template <typename T> vector<T> RUI(const vector<T> &v) {
    vector<T> res(v.size() + 1);
    for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];
    return res;
}
// 反時計周りに 90 度回転
template <typename T> void roth(vector<vector<T>> &v) {
    if(empty(v)) return;
    int n = v.size(), m = v[0].size();
    vector<vector<T>> res(m, vector<T>(n));
    rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];
    v.swap(res);
}
// 時計周りに 90 度回転
template <typename T> void rott(vector<vector<T>> &v) {
    if(empty(v)) return;
    int n = v.size(), m = v[0].size();
    vector<vector<T>> res(m, vector<T>(n));
    rep(i, n) rep(j, m) res[j][n - 1 - i] = v[i][j];
    v.swap(res);
}
bool ispow2(int i) { return i && (i & -i) == i; }
bool ispow2(ll i) { return i && (i & -i) == i; }
template <typename T, typename S> T ceil(T x, S y) { // x/y以上の最小の整数を返す
    assert(y);
    return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}
 
template <typename T, typename S> T floor(T x, S y) { // x/y以下の最大の整数を返す
    assert(y);
    return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
template <class S> vector<pair<S, int>> RunLength(const vector<S> &v) {
    vector<pair<S, int>> res;
    for(auto &e : v) {
        if(res.empty() || res.back().first != e)
            res.emplace_back(e, 1);
        else
            res.back().second++;
    }
    return res;
}
vector<pair<char, int>> RunLength(const string &v) {
    vector<pair<char, int>> res;
    for(auto &e : v) {
        if(res.empty() || res.back().first != e)
            res.emplace_back(e, 1);
        else
            res.back().second++;
    }
    return res;
}
template <class T, class F> T bin_search(T ok, T ng, const F &f) {
    while(abs(ok - ng) > 1) {
        T mid = ok + ng >> 1;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {
    while(iter--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
namespace aux {
template <typename T, unsigned N, unsigned L> struct tp {
    static void output(std::ostream &os, const T &v) {
        os << std::get<N>(v) << (&os == &cerr ? ", " : " ");
        tp<T, N + 1, L>::output(os, v);
    }
};
template <typename T, unsigned N> struct tp<T, N, N> {
    static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }
};
} // namespace aux
template <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {
    if(&os == &cerr) { os << '('; }
    aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);
    if(&os == &cerr) { os << ')'; }
    return os;
}
template <typename T> std::ostream &operator<<(std::ostream &os, const stack<T> &_st) {
    auto st = _st;
    vector<T> res;
    while(!empty(st)) res.emplace_back(st.top()), st.pop();
    reverse(all(res));
    return os << res;
}
template <typename T> std::ostream &operator<<(std::ostream &os, const queue<T> &_qu) {
    auto qu = _qu;
    vector<T> res;
    while(!empty(qu)) res.emplace_back(qu.front()), qu.pop();
    return os << res;
}
template <typename T> std::ostream &operator<<(std::ostream &os, const deque<T> &_dq) {
    auto dq = _dq;
    vector<T> res;
    while(!empty(dq)) res.emplace_back(dq.front()), dq.pop_front();
    return os << res;
}
template <typename T, typename S, typename U> std::ostream &operator<<(std::ostream &os, const priority_queue<T, S, U> &_pq) {
    auto pq = _pq;
    vector<T> res;
    while(!empty(pq)) res.emplace_back(pq.top()), pq.pop();
    return os << res;
}
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {
    if(&os == &cerr) { return os << "(" << p.first << ", " << p.second << ")"; }
    return os << p.first << " " << p.second;
}
template <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {
    bool f = true;
    if(&os == &cerr) os << "[";
    for(auto &y : x) {
        if(&os == &cerr)
            os << (f ? "" : ", ") << y;
        else
            os << (f ? "" : " ") << y;
        f = false;
    }
    if(&os == &cerr) os << "]";
    return os;
}

static uint32_t RandXor(){
    static uint32_t x=123456789;
    static uint32_t y=362436069;
    static uint32_t z=521288629;
    static uint32_t w=88675123;
    uint32_t t;
 
    t=x^(x<<11);
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}

static double Rand01(){
    return (RandXor()+0.5)*(1.0/UINT_MAX);
}

template <typename T>
void rshuffle(vector<T> &V){
    random_device seed_gen;
    mt19937 engine(seed_gen());
    shuffle(V.begin(),V.end(),engine);
}

template <typename T = int>
struct Edge{
    int from, to;
    T cost;
    int idx;
    Edge() = default;
    Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {}
    operator int() const { return to; }
};

template <typename T = int>
struct Graph{
    vector<vector<Edge<T>>> g;
    int es;
    Graph() = default;
    explicit Graph(int n) : g(n), es(0) {}
    size_t size() const{
        return g.size();
    }

    void add_directed_edge(int from, int to, T cost = 1){
        g[from].emplace_back(from, to, cost, es++);
    }

    void add_edge(int from, int to, T cost = 1){
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }

    void read(int M, int padding = 0, bool weighted = false, bool directed = false){
        for (int i = 0; i < M; i++){
            int a, b;
            cin >> a >> b;
            a += padding;
            b += padding;
            T c = T(1);
            if (weighted) cin >> c;
            if (directed) add_directed_edge(a, b, c);
            else add_edge(a, b, c);
        }
    }

    inline vector<Edge<T>> &operator[](const int &k){
        return g[k];
    }

    inline const vector<Edge<T>> &operator[](const int &k) const{
        return g[k];
    }
};

template <typename T = int>
using Edges = vector<Edge<T>>;

// merge(x,y):mergeする,未併合ならtrueが,併合済みならfalseが返ってくる
// leader(x):xの根を返す
// size(x):xの属する集合のサイズを返す
// same(x,y):x,yが同じ集合に属するかどうか
// groups():各集合に含まれる要素を返す
// number_of_groups():集合の総数を返す(O(1))

struct dsu{
public:
    dsu() : _n(0), gn(0) {}
    dsu(int n) : _n(n), gn(n), parent_or_size(n, -1) {}

    int merge(int a, int b){
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y)
            return x;
        gn--;
        if (-parent_or_size[x] < -parent_or_size[y])
            swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b){
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a){
        assert(0 <= a && a < _n);
        if (parent_or_size[a] < 0)
            return a;
        return parent_or_size[a] = leader(parent_or_size[a]);
    }

    int size(int a){
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    vector<vector<int>> groups(){
        vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++){
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        vector<vector<int>> result(_n);
        for (int i = 0; i < _n; i++){
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++){
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            remove_if(result.begin(), result.end(),
                           [&](const vector<int> &v)
                           { return v.empty(); }),
            result.end());
        return result;
    }

    int number_of_groups(){
        return gn;
    }

private:
    int _n, gn;
    // root node: -1 * component size
    // otherwise: parent
    vector<int> parent_or_size;
};

// dijkstra(g,start) とする. 返り値は以下の3つ.
// startからの最短距離の配列 dist
// 最短経路でその頂点の前に通る頂点の配列 from (startおよび到達不能頂点では-1)
// 最短経路でその頂点の前に通る辺の辺番号の配列 idx (startおよび到達不能頂点では-1)

template <typename T>
struct ShortestPath{
    vector<T> dist;
    vector<int> from, id;
};

template <typename T>
ShortestPath<T> dijkstra(const Graph<T> &g, int s){
    const auto INF = numeric_limits<T>::max();
    vector<T> dist(g.size(), INF);
    vector<int> from(g.size(), -1), id(g.size(), -1);
    using Pi = pair<T, int>;
    priority_queue<Pi, vector<Pi>, greater<>> que;
    dist[s] = 0;
    que.emplace(dist[s], s);
    while (!que.empty()){
        T cost;
        int idx;
        tie(cost, idx) = que.top();
        que.pop();
        if (dist[idx] < cost) continue;
        for (auto &e : g[idx]){
            auto next_cost = cost + e.cost;
            if (dist[e.to] <= next_cost) continue;
            dist[e.to] = next_cost;
            from[e.to] = idx;
            id[e.to] = e.idx;
            que.emplace(dist[e.to], e.to);
        }
    }
    return {dist, from, id};
}

void solve(){
    int W,H;
    cin>>W>>H;
    vs F(H);
    cin>>F;
    dsu UF(H*W);
    Graph<int> G(H*W+2);
    rep(i,H){
        rep(j,W){
            if(i!=H-1){
                if(F[i][j]==F[i+1][j]) UF.merge(W*i+j,W*(i+1)+j);
                if(F[i][j]=='#' && F[i+1][j]=='#') G.add_edge(W*i+j,W*(i+1)+j,1);
                else G.add_edge(W*i+j,W*(i+1)+j,0);
            }
            if(j!=W-1){
                if(F[i][j]==F[i][j+1]) UF.merge(W*i+j,W*i+j+1);
                if(F[i][j]=='#' && F[i][j+1]=='#') G.add_edge(W*i+j,W*i+j+1,1);
                else G.add_edge(W*i+j,W*i+j+1,0);
            }
        }
    }
    int c=0;
    for(auto g:UF.groups()){
        int h=g[0]/W, w=g[0]%W;
        if(F[h][w]=='.'){
            for(auto elm:g) G.add_edge(H*W+c,elm,0);
            c++;
        }
        else continue;
    }
    auto ret=dijkstra(G,H*W).dist;
    cout<<ret[H*W+1]+1<<"\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();
}
0