結果

問題 No.957 植林
ユーザー treeonetreeone
提出日時 2019-12-19 15:22:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,860 bytes
コンパイル時間 2,240 ms
コンパイル使用メモリ 206,228 KB
実行使用メモリ 14,164 KB
最終ジャッジ日時 2023-09-21 07:24:11
合計ジャッジ時間 5,885 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
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>
using namespace std;
typedef pair<int, int> P;
using ll = long long;
const ll INF = 1e16;

struct edge{
    int to;
    ll cap;
    int rev;
};

vector<edge> G[610];
bool used[610];

void add_edge(int from, int to, ll cap){
    G[from].push_back({to, cap, (int)G[to].size()});
    G[to].push_back({from, 0LL, (int)G[from].size() - 1});
}

ll dfs(int v, int t, ll f){
    if(v == t) return f;
    used[v] = true;
    for(int i = 0; i < G[v].size(); i++){
        edge &e = G[v][i];
        if(!used[e.to] && e.cap > 0){
            ll d = dfs(e.to, t, min(f, e.cap));
            if(d > 0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

ll max_flow(int s, int t){
    ll flow = 0;
    for(;;){
        memset(used, 0, sizeof(used));
        ll f = dfs(s, t, INF);
        if(f == 0) return flow;
        flow += f;
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int H, W;
    cin >> H >> W;
    vector<vector<ll> > G(H, vector<ll>(W));
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            cin >> G[i][j];
        }
    }
    vector<ll> R(H), C(W), S(H);
    int ans = 0;
    for(int i = 0; i < H; i++){
        cin >> R[i];
        for(int j = 0; j < W; j++){
            S[i] += G[i][j];
        }
        ans += max(R[i] - S[i], 0LL);
    }
    for(int i = 0; i < W; i++){
        cin >> C[i];
        ans += C[i];
    }
    for(int i = 0; i < H; i++){
        add_edge(0, i + 1, max(S[i] - R[i], 0LL));
    }
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            add_edge(i + 1, H + j + 1, G[i][j]);
        }
    }
    for(int i = 0; i < W; i++){
        add_edge(H + i + 1, H + W + 1, C[i]);
    }
    cout << ans - max_flow(0, H + W + 1) << endl;
}
0