結果

問題 No.957 植林
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-31 11:37:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 613 ms / 2,000 ms
コード長 3,128 bytes
コンパイル時間 1,570 ms
コンパイル使用メモリ 141,048 KB
実行使用メモリ 10,312 KB
最終ジャッジ日時 2023-10-31 11:38:00
合計ジャッジ時間 16,069 ms
ジャッジサーバーID
(参考情報)
judge11 / 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 26 ms
9,252 KB
testcase_04 AC 23 ms
8,992 KB
testcase_05 AC 27 ms
9,368 KB
testcase_06 AC 25 ms
9,784 KB
testcase_07 AC 26 ms
8,992 KB
testcase_08 AC 18 ms
9,520 KB
testcase_09 AC 18 ms
9,520 KB
testcase_10 AC 20 ms
9,520 KB
testcase_11 AC 18 ms
9,520 KB
testcase_12 AC 18 ms
9,256 KB
testcase_13 AC 15 ms
7,672 KB
testcase_14 AC 20 ms
10,204 KB
testcase_15 AC 18 ms
9,256 KB
testcase_16 AC 15 ms
7,672 KB
testcase_17 AC 16 ms
8,992 KB
testcase_18 AC 404 ms
9,256 KB
testcase_19 AC 436 ms
9,520 KB
testcase_20 AC 495 ms
9,520 KB
testcase_21 AC 471 ms
9,784 KB
testcase_22 AC 528 ms
9,784 KB
testcase_23 AC 538 ms
9,784 KB
testcase_24 AC 560 ms
10,048 KB
testcase_25 AC 602 ms
10,312 KB
testcase_26 AC 582 ms
10,312 KB
testcase_27 AC 605 ms
10,312 KB
testcase_28 AC 578 ms
10,312 KB
testcase_29 AC 613 ms
10,312 KB
testcase_30 AC 581 ms
10,312 KB
testcase_31 AC 464 ms
9,256 KB
testcase_32 AC 437 ms
9,520 KB
testcase_33 AC 477 ms
9,520 KB
testcase_34 AC 468 ms
9,784 KB
testcase_35 AC 503 ms
9,784 KB
testcase_36 AC 537 ms
9,784 KB
testcase_37 AC 560 ms
10,048 KB
testcase_38 AC 588 ms
10,312 KB
testcase_39 AC 586 ms
10,312 KB
testcase_40 AC 578 ms
10,312 KB
testcase_41 AC 13 ms
10,312 KB
testcase_42 AC 14 ms
10,312 KB
testcase_43 AC 20 ms
10,312 KB
testcase_44 AC 21 ms
10,312 KB
testcase_45 AC 1 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<random>
#include<functional>
#include<utility>
using namespace std;
struct Graph
{
    struct edge
    {
        int rev,from,to;
	long cap;
        edge(int r,int f,int t,long c) : rev(r),from(f),to(t),cap(c) {}
    };

    vector<vector<edge>> list;

    Graph(int n = 0) : list(n) {}

    size_t size() {return list.size();}

    vector<edge> &operator[](int i) {return list[i];}

    edge &redge(const edge &e) {return list[e.to][e.rev];}

    void run_flow(edge &e,long f)
    {
        assert(f <= e.cap);
        e.cap -= f;
        redge(e).cap += f;
    }

    void add_edge(int from,int to,long cap)
    {
        int fromrev = (int)list[from].size();
        int torev = (int)list[to].size();

        list[from].push_back(edge(torev,from,to,cap));
        list[to].push_back(edge(fromrev,to,from,0));
    }
};
template<class T>struct Dinic
{
	T INF;
	vector<int> level,iter;

	Dinic(T INF) : INF(INF) {}

	void bfs(Graph &G,int s)
	{
		level.assign((int)G.size(),-1);
		level[s] = 0;
		queue<int> Q;
		Q.push(s);
		while(!Q.empty())
		{
			int u = Q.front();
			Q.pop();

			for(int i = 0;i < (int)G[u].size();i++)
			{
				auto &e = G[u][i];
				if(e.cap && level[e.to] < 0)
				{
					level[e.to] = level[u] + 1;
					Q.push(e.to);
				}
			}
		}
	}
	
	T dfs(Graph &G,int u,int target,T f)
    	{
        	if(u == target) return f;

        	for(int &i = iter[u];i < (int)G[u].size();i++)
        	{
			auto &e = G[u][i];
           	 	if(!e.cap) continue;
			if(level[u] >= level[e.to]) continue;

			T flow = dfs(G,e.to,target,min(f,T(e.cap)));
			if(!flow) continue;

			G.run_flow(e,flow);

			return flow;
		}

		return 0;
   	}

	T maxflow(Graph &G,int s,int t)
	{
		T flow = 0;

		while(true)
		{
			bfs(G,s);
			if(level[t] < 0) return flow;
			iter.assign((int)G.size(),0);
			while(true)
			{
				T f = dfs(G,s,t,INF);
				if(!f) break;

				flow += f;
			}
		}
	}

	vector<bool> mincut(Graph G,int s)
	{
		vector<bool> res((int)G.size());
		queue<int> Q;
		Q.push(s);

		while(!Q.empty())
		{
			int u = Q.front();
			Q.pop();
			res[u] = true;

			for(auto e:G[u]) if(e.cap && !res[e.to]) res[e.to] = true,Q.push(e.to);
		}

		return res;
	}
};
int H,W;
long long cost[300];
void solve()
{
	cin >> H >> W;
	Graph G(H+W+2);
	int S = H+W,T = S+1;
	for(int i = 0;i < H;i++)for(int j = 0;j < W;j++)
	{
		int g;
		cin >> g;
		cost[i] += g;
		G.add_edge(H+j,i,g);
	}
	long long ans = 0;
	for(int i = 0;i < H;i++)
	{
		int R;
		cin >> R;
		ans += R;
		G.add_edge(S,i,R);
		G.add_edge(i,T,cost[i]);
	}
	for(int j = 0;j < W;j++)
	{
		int C;
		cin >> C;
		ans += C;
		G.add_edge(S,H+j,C);
	}
	Dinic<long long> dc((long long)1e18);
	long long mf = dc.maxflow(G,S,T);
	ans -= mf;
	cout << ans << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) solve();
}
0