結果

問題 No.957 植林
ユーザー ningenMeningenMe
提出日時 2021-04-18 20:03:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,309 bytes
コンパイル時間 2,899 ms
コンパイル使用メモリ 215,968 KB
実行使用メモリ 19,124 KB
最終ジャッジ日時 2023-09-17 08:02:44
合計ジャッジ時間 9,453 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,760 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 86 ms
17,348 KB
testcase_04 AC 79 ms
16,420 KB
testcase_05 AC 80 ms
18,192 KB
testcase_06 AC 88 ms
18,832 KB
testcase_07 AC 101 ms
17,108 KB
testcase_08 AC 52 ms
17,848 KB
testcase_09 AC 50 ms
17,868 KB
testcase_10 AC 53 ms
18,412 KB
testcase_11 AC 50 ms
17,888 KB
testcase_12 AC 50 ms
17,820 KB
testcase_13 AC 40 ms
16,192 KB
testcase_14 AC 46 ms
19,124 KB
testcase_15 AC 44 ms
17,636 KB
testcase_16 AC 37 ms
16,092 KB
testcase_17 AC 39 ms
16,708 KB
testcase_18 TLE -
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;
using int64   = long long;

/*
 * @title Dinic - Dinicフロー
 * @docs md/graph/Dinic.md
 */
template <class T> class Dinic {
	struct info {
		int to, rev;
		T cap;
	};
	T ini, inf;
	vector<vector<info>> edge;
	vector<int> level, iter;

	inline void bfs(int start) {
		for (int i = 0; i < level.size(); ++i) level[i] = -1;
		queue<int> q;
		level[start] = 0;
		q.push(start);
		while (q.size()) {
			int from = q.front();
			q.pop();
			for (auto& e : edge[from]) {
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[from] + 1;
					q.push(e.to);
				}
			}
		}
	}

	inline T dfs(int from, int goal, T flow) {
		if (from == goal) return flow;
		for (int& i = iter[from]; i < edge[from].size(); ++i) {
			auto& e = edge[from][i];
			if (e.cap <= 0 || level[from] >= level[e.to]) continue;
			T dflow = dfs(e.to, goal, min(flow, e.cap));
			if (dflow <= 0) continue;
			e.cap -= dflow;
			edge[e.to][e.rev].cap += dflow;
			return dflow;
		}
		return ini;
	}

public:
	Dinic(int N, T ini, T inf) : edge(N), level(N), iter(N), ini(ini), inf(inf) {
		// do nothing
	}

	inline void make_edge(int from, int to, T cap) {
		edge[from].push_back({ to, (int)edge[to].size(), cap });
		edge[to].push_back({ from, (int)edge[from].size() - 1, ini });
	}

	inline T maxflow(int start, int goal) {
		T maxflow = ini;
		while (1) {
			bfs(start);
			if (level[goal] < 0) return maxflow;
			for (int i = 0; i < iter.size(); ++i) iter[i] = 0;
			T flow;
			while ((flow = dfs(start, goal, inf))>0) maxflow += flow;
		}
	}
};

//verify https://atcoder.jp/contests/arc085/tasks/arc085_c

/**
 * @url 
 * @est
 */ 
int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    int64 inf = 1e16;
    int H,W; cin >> H >> W;
    vector<vector<int64>> grid(H,vector<int64>(W));
    for(int i=0;i<H;++i) for(int j=0;j<W;++j) cin >> grid[i][j];    
    vector<int64> R(H),C(W);
    for(int i=0;i<H;++i) cin >> R[i];
    for(int j=0;j<W;++j) cin >> C[j];    

    //行/列に対して「植える」:0, 「植えない」:1 ,と割り当てる
    Dinic<int64> di(H+W+H*W+2,0,inf);
    int S = H+W+H*W, G = H+W+H*W+1;

    int64 ans = 0;
    for(int i=0;i<H;++i) {
        //i行目を全部植えたときのコストを計算
        int64 cost = R[i];
        for(int j=0;j<W;++j) cost -= grid[i][j];
        
        //iが0のとき|cost|得る
        if(cost >= 0) ans += cost, di.make_edge(S,i,cost);
        //iが0のとき|cost|失う
        else di.make_edge(i,G,abs(cost));
    }
    for(int j=0;j<W;++j) {
        //j列目を全部植えたときのコストを計算
        int64 cost = C[j];
        for(int i=0;i<H;++i) cost -= grid[i][j];
        
        //jが0のとき|cost|得る
        if(cost >= 0) ans += cost, di.make_edge(S,j+H,cost);
        //jが0のとき|cost|失う
        else di.make_edge(j+H,G,abs(cost));
    }
    for(int i=0;i<H;++i) for(int j=0;j<W;++j) {
        //i,jが両方0のとき、コストgrid[i][j]がダブルカウントされているので
        //iが0,jが0のとき、grid[i][j]得る
        ans += grid[i][j];
        int k = H+W+i*W+j;
        di.make_edge(S,k,grid[i][j]);
        di.make_edge(k,i,inf);
        di.make_edge(k,j+H,inf);
    }
    cout << ans - di.maxflow(S,G) << endl;
    return 0;
}
0