結果

問題 No.957 植林
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-10-12 18:58:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 570 ms / 2,000 ms
コード長 2,943 bytes
コンパイル時間 3,222 ms
コンパイル使用メモリ 178,832 KB
実行使用メモリ 12,568 KB
最終ジャッジ日時 2023-09-27 23:48:21
合計ジャッジ時間 15,783 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 35 ms
10,712 KB
testcase_04 AC 34 ms
10,400 KB
testcase_05 AC 34 ms
11,040 KB
testcase_06 AC 38 ms
11,236 KB
testcase_07 AC 33 ms
10,500 KB
testcase_08 AC 27 ms
11,108 KB
testcase_09 AC 29 ms
10,924 KB
testcase_10 AC 30 ms
11,296 KB
testcase_11 AC 30 ms
10,784 KB
testcase_12 AC 29 ms
10,940 KB
testcase_13 AC 24 ms
9,100 KB
testcase_14 AC 32 ms
11,504 KB
testcase_15 AC 26 ms
10,676 KB
testcase_16 AC 24 ms
9,048 KB
testcase_17 AC 26 ms
10,452 KB
testcase_18 AC 401 ms
10,892 KB
testcase_19 AC 418 ms
11,048 KB
testcase_20 AC 428 ms
11,196 KB
testcase_21 AC 452 ms
11,624 KB
testcase_22 AC 480 ms
11,576 KB
testcase_23 AC 486 ms
11,768 KB
testcase_24 AC 494 ms
11,732 KB
testcase_25 AC 547 ms
12,372 KB
testcase_26 AC 539 ms
12,260 KB
testcase_27 AC 543 ms
12,216 KB
testcase_28 AC 533 ms
12,420 KB
testcase_29 AC 570 ms
12,352 KB
testcase_30 AC 542 ms
12,568 KB
testcase_31 AC 403 ms
10,892 KB
testcase_32 AC 424 ms
11,040 KB
testcase_33 AC 441 ms
11,208 KB
testcase_34 AC 456 ms
11,564 KB
testcase_35 AC 478 ms
11,620 KB
testcase_36 AC 491 ms
11,836 KB
testcase_37 AC 501 ms
11,684 KB
testcase_38 AC 529 ms
12,248 KB
testcase_39 AC 528 ms
12,420 KB
testcase_40 AC 538 ms
12,260 KB
testcase_41 AC 19 ms
11,980 KB
testcase_42 AC 19 ms
12,356 KB
testcase_43 AC 42 ms
12,296 KB
testcase_44 AC 43 ms
11,976 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.12 18:58:30
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


struct Dinic {
private:
    struct edge {
        int to;
        ll cap;
        int rev;
        bool isrev;
        int idx;
    };
    vector< vector< edge > > graph;
    vector< int > min_cost, iter;
    bool bfs(int s, int t) {
        min_cost.assign(graph.size(), -1);
        queue< int > que;
        min_cost[s] = 0;
        que.push(s);
        while (!que.empty() && min_cost[t] == -1) {
            int p = que.front();
            que.pop();
            for (auto &e : graph[p]) {
                if (e.cap > 0 && min_cost[e.to] == -1) {
                    min_cost[e.to] = min_cost[p] + 1;
                    que.push(e.to);
                }
            }
        }
        return min_cost[t] != -1;
    }
    ll dfs(int idx, const int t, ll flow) {
        if (idx == t) return flow;
        for (int &i = iter[idx]; i < graph[idx].size(); i++) {
            edge &e = graph[idx][i];
            if (e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
                ll d = dfs(e.to, t, min(flow, e.cap));
                if (d > 0) {
                    e.cap -= d;
                    graph[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
public:
    Dinic(int V) : graph(V) {}
    
    void add_edge(int from, int to, ll cap, int idx = -1) {
        graph[from].push_back({to, cap, (int)graph[to].size(), false, idx});
        graph[to].push_back({from, 0, (int)graph[from].size() - 1, true, idx});
    }
    
    ll max_flow(int s, int t) {
        ll flow = 0;
        while (bfs(s, t)) {
            iter.assign(graph.size(), 0);
            ll f = 0;
            while ((f = dfs(s, t, 1e9 + 6)) > 0) flow += f;
        }
        return flow;
    }
    void output() {
        for (int i = 0; i < graph.size(); i++) {
            for (auto &e : graph[i]) {
                if (e.isrev) continue;
                auto &rev_e = graph[e.to][e.rev];
                cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
            }
        }
    }
};
int main() {
    int h,w;cin >> h >> w;
    vector<vector<int>> a(h,vector<int>(w));
    int s = h+w;
    int t = h+w+1;
    Dinic g(h+w+2);
    for (int i = 0; i < h; i++) {
        ll sum = 0;
        for (int j = 0; j < w; j++) {
            cin >> a[i][j];
            sum += a[i][j];
            g.add_edge(i,h+j,a[i][j]);
        }
        g.add_edge(s,i,sum);
    }
    vector<int> r(h),c(w);
    ll ans = 0;
    for (int i = 0; i < h; i++) {
        cin >> r[i];
        ans += r[i];
        g.add_edge(i,t,r[i]);
    }
    for (int i = 0; i < w; i++) {
        cin >> c[i];
        ans += c[i];
        g.add_edge(h+i,t,c[i]);
    }
    cout << ans - g.max_flow(s,t) << endl;
    return 0;
}
0