結果

問題 No.957 植林
ユーザー ningenMeningenMe
提出日時 2023-07-18 04:35:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 540 ms / 2,000 ms
コード長 7,860 bytes
コンパイル時間 3,305 ms
コンパイル使用メモリ 225,848 KB
実行使用メモリ 11,196 KB
最終ジャッジ日時 2023-10-18 07:44:58
合計ジャッジ時間 16,650 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 22 ms
9,928 KB
testcase_04 AC 18 ms
9,612 KB
testcase_05 AC 23 ms
9,924 KB
testcase_06 AC 22 ms
10,148 KB
testcase_07 AC 22 ms
9,664 KB
testcase_08 AC 15 ms
10,388 KB
testcase_09 AC 14 ms
10,364 KB
testcase_10 AC 16 ms
10,140 KB
testcase_11 AC 14 ms
10,104 KB
testcase_12 AC 14 ms
10,140 KB
testcase_13 AC 11 ms
8,292 KB
testcase_14 AC 15 ms
10,668 KB
testcase_15 AC 13 ms
9,840 KB
testcase_16 AC 12 ms
8,724 KB
testcase_17 AC 13 ms
9,520 KB
testcase_18 AC 378 ms
10,332 KB
testcase_19 AC 405 ms
10,312 KB
testcase_20 AC 438 ms
10,176 KB
testcase_21 AC 425 ms
10,228 KB
testcase_22 AC 465 ms
10,668 KB
testcase_23 AC 502 ms
10,668 KB
testcase_24 AC 530 ms
10,932 KB
testcase_25 AC 539 ms
10,932 KB
testcase_26 AC 537 ms
10,932 KB
testcase_27 AC 534 ms
10,932 KB
testcase_28 AC 534 ms
10,932 KB
testcase_29 AC 538 ms
10,932 KB
testcase_30 AC 538 ms
10,932 KB
testcase_31 AC 378 ms
10,332 KB
testcase_32 AC 412 ms
10,312 KB
testcase_33 AC 438 ms
10,176 KB
testcase_34 AC 436 ms
10,228 KB
testcase_35 AC 471 ms
10,668 KB
testcase_36 AC 500 ms
10,668 KB
testcase_37 AC 527 ms
10,932 KB
testcase_38 AC 540 ms
10,932 KB
testcase_39 AC 536 ms
10,932 KB
testcase_40 AC 532 ms
10,932 KB
testcase_41 AC 11 ms
11,196 KB
testcase_42 AC 11 ms
11,196 KB
testcase_43 AC 14 ms
10,932 KB
testcase_44 AC 14 ms
10,932 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 int128 = __int128_t;
using int64 = long long;

#define ALL(obj) (obj).begin(),(obj).end()

/*
 * @title FastIO
 * @docs md/util/FastIO.md
 */
class FastIO{
private:
    inline static constexpr int ch_0='0';
    inline static constexpr int ch_9='9';
    inline static constexpr int ch_n='-';
    inline static constexpr int ch_s=' ';
    inline static constexpr int ch_l='\n';
    inline static void endline_skip(char& ch) {
        while(ch==ch_l) ch=getchar();
    }
    template<typename T> inline static void read_integer(T &x) {
        int neg=0; char ch; x=0;
        ch=getchar();
        endline_skip(ch);
        if(ch==ch_n) neg=1,ch=getchar();
        for(;(ch_0 <= ch && ch <= ch_9); ch = getchar()) x = x*10 + (ch-ch_0);
        if(neg) x*=-1;
    }
    template<typename T> inline static void read_unsigned_integer(T &x) {
        char ch; x=0;
        ch=getchar();
        endline_skip(ch);
        for(;(ch_0 <= ch && ch <= ch_9); ch = getchar()) x = x*10 + (ch-ch_0);
    }
    inline static void read_string(string &x) {
        char ch; x="";
        ch=getchar();
        endline_skip(ch);
        for(;(ch != ch_s && ch!=ch_l); ch = getchar()) x.push_back(ch);
    }
    inline static char ar[40];
    inline static char *ch_ar;
    template<typename T> inline static void write_integer(T x) {
        ch_ar=ar;
        if(x< 0) putchar(ch_n), x=-x;
        if(x==0) putchar(ch_0);
        for(;x;x/=10) *ch_ar++=(ch_0+x%10);
        while(ch_ar--!=ar) putchar(*ch_ar);
    }
public:
    inline static void read(int &x) {read_integer<int>(x);}
    inline static void read(long long &x) {read_integer<long long>(x);}
    inline static void read(unsigned int &x) {read_unsigned_integer<unsigned int>(x);}
    inline static void read(unsigned long long &x) {read_unsigned_integer<unsigned long long>(x);}
    inline static void read(string &x) {read_string(x);}
    inline static void read(__int128_t &x) {read_integer<__int128_t>(x);}
    inline static void write(__int128_t x) {write_integer<__int128_t>(x);}
    inline static void write(char x) {putchar(x);}
};
#define read(arg) FastIO::read(arg)
#define write(arg) FastIO::write(arg)

template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL;
constexpr int64 LOWINF = 1'000'000'000'000'000LL; //'
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;}
inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b)
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

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

	inline void bfs(const size_t start) {
		for (int i = 0; i < level.size(); ++i) level[i] = -1;
		queue<size_t> q;
		level[start] = 0;
		q.push(start);
		while (q.size()) {
			auto 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(size_t from, size_t 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 0;
	}

public:
	DinicMaxFlow(const size_t N) : edge(N), level(N), iter(N), inf_flow(0) {
		// do nothing
	}

	inline void make_edge(const size_t from,const size_t to, T cap) {
		edge[from].push_back({ to, edge[to].size(), cap });
		edge[to].push_back({ from, edge[from].size() - 1, 0});
        inf_flow += cap;
	}

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

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

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

    //行/列に対して「植える」:0, 「植えない」:1 ,と割り当てる
    //加えて、問題の性質的にi行目に関してはフローを流す前に貪欲に選んでしまう
    //そうすることで、辺の数が2乗オーダーから落ちる
    DinicMaxFlow<int64> di(H+W+2);
    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.flow(S,G) << endl;
    return 0;
}
0