結果

問題 No.957 植林
ユーザー ningenMeningenMe
提出日時 2023-07-18 04:16:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 527 ms / 2,000 ms
コード長 3,118 bytes
コンパイル時間 4,453 ms
コンパイル使用メモリ 218,892 KB
実行使用メモリ 9,056 KB
最終ジャッジ日時 2023-10-18 07:25:01
合計ジャッジ時間 15,256 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 23 ms
8,492 KB
testcase_04 AC 20 ms
8,000 KB
testcase_05 AC 24 ms
8,532 KB
testcase_06 AC 22 ms
8,680 KB
testcase_07 AC 23 ms
8,408 KB
testcase_08 AC 16 ms
8,584 KB
testcase_09 AC 16 ms
8,528 KB
testcase_10 AC 18 ms
8,548 KB
testcase_11 AC 16 ms
8,528 KB
testcase_12 AC 15 ms
8,264 KB
testcase_13 AC 14 ms
6,944 KB
testcase_14 AC 17 ms
8,792 KB
testcase_15 AC 16 ms
8,552 KB
testcase_16 AC 14 ms
6,944 KB
testcase_17 AC 15 ms
8,264 KB
testcase_18 AC 368 ms
8,596 KB
testcase_19 AC 396 ms
8,628 KB
testcase_20 AC 428 ms
8,680 KB
testcase_21 AC 429 ms
8,744 KB
testcase_22 AC 454 ms
8,804 KB
testcase_23 AC 480 ms
8,856 KB
testcase_24 AC 502 ms
8,908 KB
testcase_25 AC 524 ms
9,056 KB
testcase_26 AC 520 ms
9,056 KB
testcase_27 AC 523 ms
9,056 KB
testcase_28 AC 523 ms
9,056 KB
testcase_29 AC 518 ms
9,056 KB
testcase_30 AC 522 ms
9,056 KB
testcase_31 AC 366 ms
8,596 KB
testcase_32 AC 394 ms
8,628 KB
testcase_33 AC 427 ms
8,680 KB
testcase_34 AC 426 ms
8,744 KB
testcase_35 AC 454 ms
8,804 KB
testcase_36 AC 478 ms
8,856 KB
testcase_37 AC 509 ms
8,908 KB
testcase_38 AC 527 ms
9,056 KB
testcase_39 AC 522 ms
9,056 KB
testcase_40 AC 522 ms
9,056 KB
testcase_41 AC 13 ms
9,056 KB
testcase_42 AC 13 ms
9,056 KB
testcase_43 AC 19 ms
9,056 KB
testcase_44 AC 19 ms
9,056 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

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 ,と割り当てる
    //加えて、問題の性質的にi行目に関してはフローを流す前に貪欲に選んでしまう
    //そうすることで、辺の数が2乗オーダーから落ちる
    Dinic<int64> di(H+W+2,0,inf);
    int S = H+W, G = H+W+1;

    int64 ans = 0;
    for(int i=0;i<H;++i) {
        //i行目を全部植えたときのコストを計算
        int64 cost = 0;
        for(int j=0;j<W;++j) cost += grid[i][j];        
        //差分が正のとき
        if(R[i]-cost >= 0) ans += R[i]-cost;
        //差分が負のとき→ iが0のとき|cost-R[i]|失う
        else di.make_edge(i,G,abs(R[i]-cost));
    }
    for(int j=0;j<W;++j) {
        //j列目を全部植えたときのコスト
        int64 cost = C[j];
        //jが0のとき|cost|得る
        ans += cost, di.make_edge(S,j+H,cost);
    }
    for(int i=0;i<H;++i) for(int j=0;j<W;++j) {
        //iが1,jが0のとき、grid[i][j]失う
        di.make_edge(j+H,i,grid[i][j]);
    }
    cout << ans - di.maxflow(S,G) << endl;
    return 0;
}
0