結果

問題 No.957 植林
ユーザー milanis48663220milanis48663220
提出日時 2021-06-02 00:54:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,215 bytes
コンパイル時間 1,754 ms
コンパイル使用メモリ 131,392 KB
実行使用メモリ 26,732 KB
最終ジャッジ日時 2024-04-26 09:58:34
合計ジャッジ時間 6,510 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,752 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,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 <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

const ll INF = 1e15;

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

class FordFolkerson{
    public:
        FordFolkerson(ll n){
            N = n;
            G = vector<vector<edge>>(n, vector<edge>());
            used = vector<bool>(n, false);
        }

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

        ll max_flow(ll s, ll t){
            ll flow = 0;
            while(true){
                clear_used();
                ll f = dfs(s, t, INF);
                if(f == 0){
                    break;
                }
                flow += f;
            }
            return flow;
        }

    private:
        vector<vector<edge>> G;
        vector<bool> used;
        ll N;
        void clear_used(){
            for(ll i = 0; i < N; i++) used[i] = false;
        }

        ll dfs(ll v, ll t, ll f){
            if(v == t) return f;
            used[v] = true;
            for(ll 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;
        }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    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<int> r(h), c(w);
    for(int i = 0; i < h; i++) cin >> r[i];
    for(int i = 0; i < w; i++) cin >> c[i];
    FordFolkerson mf(2+h+w+h*w);
    int s = 0, t = 1+h+w+h*w;
    auto row_index = [&](int i){ return i+1; };
    auto col_index = [&](int j){ return h+j; };
    auto cell_index = [&](int i, int j){ return h+w+w*i+j; };
    for(int i = 0; i < h; i++) mf.add_edge(s, row_index(i), r[i]);
    for(int j = 0; j < w; j++) mf.add_edge(s, col_index(j), c[j]);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            mf.add_edge(row_index(i), cell_index(i, j), INF);
            mf.add_edge(col_index(j), cell_index(i, j), INF);
            mf.add_edge(cell_index(i, j), t, g[i][j]);
        }
    }
    cout << accumulate(r.begin(), r.end(), 0ll)+accumulate(c.begin(), c.end(), 0ll)-mf.max_flow(s, t) << endl;
}
0