cp-documentation

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub zawa-tin/cp-documentation

:heavy_check_mark: AOJ2943 イルミネーション
(Test/AOJ/2943.test.cpp)

燃やす埋める。

割当による損得の関係を見極める

電源装置をつけると $W$ の損

電源装置をつけると $s = B_{(i, j)} + B_{(i + 1,j)} + B_{(i, j + 1)} + B_{(i + 1, j + 1)}$ の得

この損得をまとめて、電源装置をつけると $s - W$ の得とする。これは負になりうる可能性があり、(最小カットに帰着したいので負になるのは困るので) $\text{INF} - (s - W)$ の損とする

電源装置をつけないと $\text{INF}$ の損

-> つける場合に $\text{INF}$ を使ったので、こっちも揃える。

$(i + 0.5, j + 0.5)$ にある電源装置を付けて、 $(i + 1.5, j + 1.5)$ にある電源装置をつけると $B_{(i + 1, j + 1)}$ の損 ( $B_{(i + 1, j + 1)}$ が2回寄与しているのを打ち消すための「損」表現

$(i + 0.5, j + 0.5$ にある電源装置を付けて、 $(i + 1.5, j - 0.5)$ にある電源装置をつけると $B_{(i + 1, j)}$ の損

得の最大化を損の最小化に言い換える

よって損をネットワークの容量とする

関係が二部グラフになっていることに注目する

全ての電源装置を source -> 電源装置 を「つける」、 電源装置 -> sink を「付けない」とすると困る。

なぜなら「両方つけると損」という関係を上手く張れないから

そこで、関係が電源装置の存在する行に注目すると二部グラフの様になっていることがわかる。

よって奇数行目にある電源装置は 電源装置 -> sinkを「つける」source -> 電源装置を「つけない」と反転すると上手くいく

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2943"

#include "../../Src/Template/IOSetting.hpp"
#include "../../Src/Graph/Flow/Dinic.hpp"

#include <cassert>
#include <iostream>
#include <vector>

int main() {
    using namespace zawa;
    SetFastIO();
    int h, w; std::cin >> h >> w;
    long long W; std::cin >> W;
    std::vector B(h, std::vector<long long>(w));
    for (auto& b : B) for (auto& x : b) std::cin >> x;
    auto exist{[&](int x, int y) -> bool {
        return x + 1 < h and y + 1 < w and (x + y) % 2 == 0;
    }};
    auto f{[&](int x, int y) -> int {
        return x * w + y;
    }};
    Dinic<long long> mf(h * w + 2);
    int source{h * w}, sink{source + 1};
    const long long INF{(long long)1e12};
    int count{};
    for (int i{} ; i < h ; i++) {
        for (int j{} ; j < w ; j++) {
            if (!exist(i, j)) continue;
            count++;
            if (i % 2 == 0) {
                mf.addEdge(source, f(i, j), 
                        INF - (-W + B[i][j] + B[i + 1][j] + B[i][j + 1] + B[i + 1][j + 1]));
                mf.addEdge(f(i, j), sink, INF);
                if (i - 1 >= 0 and j - 1 >= 0) {
                    assert(exist(i - 1, j - 1));
                    mf.addEdge(f(i - 1, j - 1), f(i, j), B[i][j]);
                }
                if (i - 1 >= 0 and j + 1 < w - 1) {
                    assert(exist(i - 1, j + 1));
                    mf.addEdge(f(i - 1, j + 1), f(i, j), B[i][j + 1]);
                }
            }
            else {
                mf.addEdge(f(i, j), sink, 
                        INF - (-W + B[i][j] + B[i + 1][j] + B[i][j + 1] + B[i + 1][j + 1]));
                mf.addEdge(source, f(i, j), INF);
                if (i - 1 >= 0 and j - 1 >= 0) {
                    assert(exist(i - 1, j - 1));
                    mf.addEdge(f(i, j), f(i - 1, j - 1), B[i][j]);
                }
                if (i - 1 >= 0 and j + 1 < w - 1) {
                    assert(exist(i - 1, j + 1));
                    mf.addEdge(f(i, j), f(i - 1, j + 1), B[i][j + 1]);
                }
            }
        }
    }
    long long ans{mf.flow(source, sink)};
    ans -= count * INF;
    ans = -ans;
    std::cout << ans << '\n';
}
#line 1 "Test/AOJ/2943.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2943"

#line 2 "Src/Template/IOSetting.hpp"

#line 2 "Src/Template/TypeAlias.hpp"

#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa
#line 4 "Src/Template/IOSetting.hpp"

#include <iostream>
#include <iomanip>

namespace zawa {

void SetFastIO() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
}

void SetPrecision(u32 dig) {
    std::cout << std::fixed << std::setprecision(dig);
}

} // namespace zawa
#line 2 "Src/Graph/Flow/Dinic.hpp"

#line 2 "Src/Utility/U32Pair.hpp"

#line 4 "Src/Utility/U32Pair.hpp"

#include <functional>
#line 7 "Src/Utility/U32Pair.hpp"

namespace zawa {

class U32Pair {
private:
    static constexpr u32 SHIFT{32};
    static constexpr u32 MASK{static_cast<u32>((1LL << SHIFT) - 1)};
    u64 value_{};
public:
    constexpr U32Pair() {}
    constexpr U32Pair(u32 first, u32 second) {
        value_ = (static_cast<u64>(first) << SHIFT) | second;
    }
    constexpr u32 first() const noexcept {
        return static_cast<u32>(value_ >> SHIFT);
    }
    constexpr u32 second() const noexcept {
        return static_cast<u32>(value_ & MASK);
    }
    constexpr u64 combined() const noexcept {
        return value_;
    }
    constexpr U32Pair& operator=(const U32Pair& rhs) {
        value_ = rhs.value_;
        return *this;
    }
    friend constexpr bool operator==(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ == rhs.value_;
    }
    friend constexpr bool operator!=(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ != rhs.value_;
    }
    friend constexpr bool operator<(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ < rhs.value_;
    }
    friend constexpr bool operator<=(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ <= rhs.value_;
    }
    friend constexpr bool operator>(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ > rhs.value_;
    }
    friend constexpr bool operator>=(const U32Pair& lhs, const U32Pair& rhs) {
        return lhs.value_ >= rhs.value_;
    }
    friend std::ostream& operator<<(std::ostream& os, const U32Pair& pair) {
        os << '(' << pair.first() << ',' << pair.second() << ')';
        return os;
    }
};

struct U32PairHash {
    usize operator()(const U32Pair& pair) const noexcept {
        return std::hash<u64>{}(pair.combined());
    }
};

} // namespace zawa
#line 5 "Src/Graph/Flow/Dinic.hpp"

#include <algorithm>
#include <cassert>
#include <limits>
#include <type_traits>
#include <vector>
#include <queue>

namespace zawa {

template <class Cap> 
class Dinic {
private:
    static_assert(std::is_signed_v<Cap>, "Cap must be signed");
    usize n_{}, m_{};
    static constexpr u32 invalid() noexcept {
        return std::numeric_limits<u32>::max();
    }
public:
    inline usize size() const noexcept {
        return n_;
    }
    inline usize edgeNumber() const noexcept {
        return m_;
    }
private:
    struct Edge {
        u32 to{}, rev{};
        Cap residual{};
        Edge() = default;
        Edge(u32 to, u32 rev, const Cap& residual) 
            : to{to}, rev{rev}, residual{residual} {}
    };

    std::vector<std::vector<Edge>> g_;
    std::vector<U32Pair> edges_;
    std::vector<u32> label_, cur_;

    bool dualStep(u32 s, u32 t) {
        std::fill(label_.begin(), label_.end(), invalid());
        label_[s] = 0;
        std::queue<u32> queue{ { s } };
        while (queue.size()) {
            u32 v{queue.front()};
            queue.pop();
            for (const Edge& e : g_[v]) if (e.residual > 0) {
                if (label_[e.to] > label_[v] + 1) {
                    label_[e.to] = label_[v] + 1;
                    if (e.to == t) return true;
                    queue.emplace(e.to);
                }
            }
        }
        return false;
    }

    bool admissible(u32 v, const Edge& e) const noexcept {
        return e.residual > 0 and label_[v] + 1 == label_[e.to];
    }

    inline void flow(Edge& e, Cap f) {
        e.residual -= f;
        g_[e.to][e.rev].residual += f;
    }

    Cap dfs(u32 v, u32 t, Cap up) {
        if (v == t) return up;
        Cap res{};
        for (u32& i{cur_[v]} ; i < g_[v].size() ; i++) {
            if (!admissible(v, g_[v][i])) continue;
            Cap f{dfs(g_[v][i].to, t, std::min(g_[v][i].residual, up - res))};
            if (f == 0) continue;
            flow(g_[v][i], f);
            res += f;
            if (res == up) return res;
        }
        return res;
    }

    Cap primalStep(u32 s, u32 t) {
        std::fill(cur_.begin(), cur_.end(), 0u);
        cur_[t] = g_[t].size();
        Cap res{};
        while (true) {
            Cap f{dfs(s, t, std::numeric_limits<Cap>::max())};
            if (f == 0) break;
            res += f;
        }
        return res;
    }

    const Edge& edge(u32 i) const noexcept {
        return g_[edges_[i].first()][edges_[i].second()];
    }
    const Edge& reverse(u32 i) const noexcept {
        const Edge& e{edge(i)};
        return g_[e.to][e.rev];
    }

public:
    Dinic() = default;
    Dinic(u32 n, u32 m = 0u) 
        : n_{n}, m_{m}, g_(n), edges_{}, label_(n), cur_(n) {
        g_.shrink_to_fit();
        label_.shrink_to_fit();
        cur_.shrink_to_fit();
        edges_.reserve(m);
    }

    u32 addEdge(u32 u, u32 v, const Cap& cap) {
        assert(u < size());
        assert(v < size());
        u32 id{static_cast<u32>(g_[u].size())};
        u32 revId{u == v ? id + 1 : static_cast<u32>(g_[v].size())};
        u32 res{static_cast<u32>(edges_.size())};
        edges_.emplace_back(u, id);
        g_[u].emplace_back(v, revId, cap);
        g_[v].emplace_back(u, id, Cap{});
        m_++;
        return res;
    }

    const Cap& flowed(u32 id) const noexcept {
        assert(id < edgeNumber());
        return reverse(id).residual;
    }
    const Cap& residual(u32 id) const noexcept {
        assert(id < edgeNumber());
        return edge(id).residual;
    }
    const Cap& capacity(u32 id) const noexcept {
        assert(id < edgeNumber());
        return edge(id).residual + reverse(id).residual;
    }
    const u32& from(u32 id) const noexcept {
        assert(id < edgeNumber());
        return edges_[id].first();
    }
    const u32& to(u32 id) const noexcept {
        assert(id < edgeNumber());
        return edge(id).to;
    }

    Cap flow(u32 s, u32 t) {
        assert(s < size());
        assert(t < size()); 
        Cap res{};
        while (dualStep(s, t)) {
            res += primalStep(s, t);
        }
        return res;
    }

    std::vector<bool> cut(u32 s) {
        std::vector<bool> res(size());
        res[s] = true;
        std::queue<u32> queue{ { s } };
        while (queue.size()) {
            u32 v{queue.front()};
            queue.pop();
            for (const auto& e : g_[v]) if (e.residual > 0 and !res[e.to]) {
                res[e.to] = true;
                queue.emplace(e.to);
            }
        }
        return res;
    }    
};

} // namespace zawa
#line 5 "Test/AOJ/2943.test.cpp"

#line 9 "Test/AOJ/2943.test.cpp"

int main() {
    using namespace zawa;
    SetFastIO();
    int h, w; std::cin >> h >> w;
    long long W; std::cin >> W;
    std::vector B(h, std::vector<long long>(w));
    for (auto& b : B) for (auto& x : b) std::cin >> x;
    auto exist{[&](int x, int y) -> bool {
        return x + 1 < h and y + 1 < w and (x + y) % 2 == 0;
    }};
    auto f{[&](int x, int y) -> int {
        return x * w + y;
    }};
    Dinic<long long> mf(h * w + 2);
    int source{h * w}, sink{source + 1};
    const long long INF{(long long)1e12};
    int count{};
    for (int i{} ; i < h ; i++) {
        for (int j{} ; j < w ; j++) {
            if (!exist(i, j)) continue;
            count++;
            if (i % 2 == 0) {
                mf.addEdge(source, f(i, j), 
                        INF - (-W + B[i][j] + B[i + 1][j] + B[i][j + 1] + B[i + 1][j + 1]));
                mf.addEdge(f(i, j), sink, INF);
                if (i - 1 >= 0 and j - 1 >= 0) {
                    assert(exist(i - 1, j - 1));
                    mf.addEdge(f(i - 1, j - 1), f(i, j), B[i][j]);
                }
                if (i - 1 >= 0 and j + 1 < w - 1) {
                    assert(exist(i - 1, j + 1));
                    mf.addEdge(f(i - 1, j + 1), f(i, j), B[i][j + 1]);
                }
            }
            else {
                mf.addEdge(f(i, j), sink, 
                        INF - (-W + B[i][j] + B[i + 1][j] + B[i][j + 1] + B[i + 1][j + 1]));
                mf.addEdge(source, f(i, j), INF);
                if (i - 1 >= 0 and j - 1 >= 0) {
                    assert(exist(i - 1, j - 1));
                    mf.addEdge(f(i, j), f(i - 1, j - 1), B[i][j]);
                }
                if (i - 1 >= 0 and j + 1 < w - 1) {
                    assert(exist(i - 1, j + 1));
                    mf.addEdge(f(i, j), f(i - 1, j + 1), B[i][j + 1]);
                }
            }
        }
    }
    long long ans{mf.flow(source, sink)};
    ans -= count * INF;
    ans = -ans;
    std::cout << ans << '\n';
}
Back to top page