結果

問題 No.957 植林
ユーザー tarattata1tarattata1
提出日時 2019-12-20 10:24:28
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,336 bytes
コンパイル時間 632 ms
コンパイル使用メモリ 79,420 KB
実行使用メモリ 17,760 KB
最終ジャッジ日時 2023-09-22 07:10:48
合計ジャッジ時間 4,752 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,760 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 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 <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;
using namespace std;
 


struct Edge{
    int to, cap, rev;
    Edge(int t, int c, int r):to(t), cap(c), rev(r) {}
};

void AddEdge(int fr, int to, int cap, vector<vector<Edge> >& gr)
{
    gr[fr].push_back( Edge(to, cap, gr[to].size()) );
    gr[to].push_back( Edge(fr, 0, gr[fr].size()-1) );
}

int dfs(int v, int G, int f, vector<vector<Edge> >& gr, vector<int>& used )
{
    if(v==G) return f;
    used[v]=1;
    auto it = gr[v].begin();
    for(; it!=gr[v].end(); ++it) {
        if(!used[it->to] && it->cap>0) {
            int d = dfs( it->to, G, MIN(f, it->cap), gr, used);
            if(d>0) {
                it->cap -= d;
                gr[it->to][it->rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow( int S, int G, vector<vector<Edge> >& g)
{
    int flow=0;
    while(1) {
        vector<int> used(g.size(), 0);
        int f = dfs(S, G, INF, g, used);
        if(f==0) break;
        flow += f;
    }
    return flow;
}

int main(int argc, char* argv[])
{
    int h,w;
    scanf("%d%d", &h, &w);
 
    ll sum=0;
    int m=h+w+h*w;
    vector<vector<Edge> > gr(m+2);
    int i,j;
    for(i=0; i<h; i++) {
        for(j=0; j<w; j++) {
            int p;
            scanf("%d", &p);
            //sum+=p;
            AddEdge(i*w+j, m+1, p, gr);
        }
    }
    for(i=0; i<h; i++) {
        int a;
        scanf("%d", &a);
        sum+=a;
        AddEdge( m, h*w+i, a, gr);
        for(j=0; j<w; j++) {
            AddEdge( h*w+i, i*w+j, INF, gr);
        }
    }
    for(i=0; i<w; i++) {
        int a;
        scanf("%d", &a);
        sum+=a;
        AddEdge( m, h*w+h+i, a, gr);
        for(j=0; j<h; j++) {
            AddEdge( h*w+h+i, j*w+i, INF, gr);
        }
    }

    int f = max_flow( m, m+1, gr);
    printf("%lld\n", sum-f);
 
    return 0;
}

0