結果

問題 No.957 植林
ユーザー 👑 hitonanodehitonanode
提出日時 2019-12-19 22:41:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,759 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 189,484 KB
実行使用メモリ 27,956 KB
最終ジャッジ日時 2023-09-21 07:34:47
合計ジャッジ時間 7,436 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,760 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 102 ms
22,060 KB
testcase_04 AC 86 ms
21,140 KB
testcase_05 AC 94 ms
22,800 KB
testcase_06 AC 103 ms
24,368 KB
testcase_07 AC 106 ms
21,236 KB
testcase_08 AC 58 ms
22,800 KB
testcase_09 AC 54 ms
22,712 KB
testcase_10 AC 70 ms
23,344 KB
testcase_11 AC 67 ms
23,268 KB
testcase_12 AC 63 ms
22,872 KB
testcase_13 AC 46 ms
20,008 KB
testcase_14 AC 60 ms
25,260 KB
testcase_15 AC 56 ms
23,132 KB
testcase_16 AC 47 ms
20,220 KB
testcase_17 AC 49 ms
21,204 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx")
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
///// This part below is only for debug, not used /////
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
///// END /////

class Maxflow
{
    using T = lint;
    struct edge { int to; T cap; int rev; };
    std::vector<std::vector<edge> > edges;
    std::vector<int> level;
    std::vector<int> iter;

    void bfs(int s)
    {
        level = std::vector<int>(edges.size(), -1);
        std::queue<int> q;
        level[s] = 0;
        q.push(s);
        while (!q.empty())
        {
            int v = q.front();
            q.pop();
            for (edge &e : edges[v])
            {
                if (e.cap > 0 && level[e.to] < 0)
                {
                    level[e.to] = level[v] + 1;
                    q.push(e.to);
                }
            }
        }
    }

    T dfs_d(int v, int goal, T f)
    {
        if (v == goal) return f;
        for (int &i = iter[v]; i < (int)edges[v].size(); i++)
        {
            edge &e = edges[v][i];
            if (e.cap > 0 && level[v] < level[e.to])
            {
                T d = dfs_d(e.to, goal, std::min(f, e.cap));
                if (d > 0)
                {
                    e.cap -= d;
                    edges[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

public:
    Maxflow(int N) { edges.resize(N); }
    void add_edge(int from, int to, T capacity)
    {
        edges[from].push_back(edge{to, capacity, (int)edges[to].size()});
        edges[to].push_back(edge{from, (T)0, (int)edges[from].size() - 1});
    }

    T Dinic(int s, int t)
    {
        constexpr T INF = std::numeric_limits<T>::max();
        T flow = 0;
        while (true)
        {
            bfs(s);
            if (level[t] < 0) return flow;
            iter = std::vector<int>(edges.size(), 0);
            T f;
            while ((f = dfs_d(s, t, INF)) > 0) flow += f;
        }
    }
};

int main()
{
    int H, W;
    cin >> H >> W;
    vector<vector<lint>> G(H, vector<lint>(W));
    cin >> G;
    vector<lint> R(H), C(W);
    cin >> R >> C;
    int Z = 1 + H + W + H * W;
    Maxflow g(Z + 1);
    REP(i, H) if (R[i]) g.add_edge(0, i + 1,R[i]);
    REP(j, W) if (C[j]) g.add_edge(0, H + 1 + j, C[j]);
    REP(i, H) REP(j, W) {
        int b = H + W + 1 + i * W + j;
        if (G[i][j] < R[i] + C[j])
        {
            g.add_edge(i + 1, b, 1e10);
            g.add_edge(H + 1 + j, b, 1e10);
            g.add_edge(b, Z, G[i][j]);
        }
        else
        {
            g.add_edge(i + 1, Z, 1e10);
            g.add_edge(H + j + 1, Z, 1e10);
        }
    }
    lint f = g.Dinic(0, Z);
    cout << accumulate(ALL(R), 0LL) + accumulate(ALL(C), 0LL) - f << endl;
}
0