結果

問題 No.957 植林
ユーザー 👑 hitonanodehitonanode
提出日時 2021-08-25 03:35:29
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 507 ms / 2,000 ms
コード長 3,626 bytes
コンパイル時間 4,053 ms
コンパイル使用メモリ 269,836 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-27 15:28:59
合計ジャッジ時間 16,153 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 19 ms
9,984 KB
testcase_04 AC 19 ms
9,600 KB
testcase_05 AC 18 ms
9,984 KB
testcase_06 AC 22 ms
10,368 KB
testcase_07 AC 18 ms
9,600 KB
testcase_08 AC 12 ms
10,112 KB
testcase_09 AC 13 ms
9,984 KB
testcase_10 AC 15 ms
10,368 KB
testcase_11 AC 14 ms
9,984 KB
testcase_12 AC 13 ms
9,984 KB
testcase_13 AC 10 ms
8,320 KB
testcase_14 AC 13 ms
10,240 KB
testcase_15 AC 15 ms
9,728 KB
testcase_16 AC 12 ms
8,064 KB
testcase_17 AC 12 ms
9,472 KB
testcase_18 AC 366 ms
9,600 KB
testcase_19 AC 387 ms
9,984 KB
testcase_20 AC 398 ms
10,240 KB
testcase_21 AC 407 ms
10,496 KB
testcase_22 AC 441 ms
10,368 KB
testcase_23 AC 456 ms
10,496 KB
testcase_24 AC 461 ms
10,752 KB
testcase_25 AC 507 ms
11,008 KB
testcase_26 AC 496 ms
11,008 KB
testcase_27 AC 505 ms
11,008 KB
testcase_28 AC 503 ms
11,008 KB
testcase_29 AC 494 ms
11,008 KB
testcase_30 AC 505 ms
11,008 KB
testcase_31 AC 371 ms
9,600 KB
testcase_32 AC 386 ms
10,112 KB
testcase_33 AC 398 ms
10,496 KB
testcase_34 AC 455 ms
10,496 KB
testcase_35 AC 453 ms
10,496 KB
testcase_36 AC 447 ms
10,496 KB
testcase_37 AC 474 ms
10,752 KB
testcase_38 AC 496 ms
11,008 KB
testcase_39 AC 502 ms
11,008 KB
testcase_40 AC 498 ms
11,008 KB
testcase_41 AC 12 ms
11,008 KB
testcase_42 AC 11 ms
11,008 KB
testcase_43 AC 18 ms
10,880 KB
testcase_44 AC 19 ms
11,008 KB
testcase_45 AC 1 ms
5,376 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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;
    lint tot = accumulate(ALL(R), 0LL) + accumulate(ALL(C), 0LL);

    vector<lint> EW(W);
    lint f1 = 0;

    int Z = 1 + H + W;
    Maxflow g(Z + 1);

    REP(i, H)
    {
        lint gtot = accumulate(ALL(G[i]), 0LL);
        lint f0 = min(gtot, R[i]);
        f1 += f0;
        g.add_edge(0, i + 1, gtot - f0);
    }
    REP(j, W) g.add_edge(H + 1 + j, Z, C[j]);
    REP(i, H) REP(j, W) g.add_edge(i + 1, H + 1 + j, G[i][j]);
    lint f2 = g.Dinic(0, Z);
    cout << tot - f1 - f2 << endl;
}
0