結果

問題 No.957 植林
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-31 11:01:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,279 bytes
コンパイル時間 1,676 ms
コンパイル使用メモリ 143,784 KB
実行使用メモリ 41,324 KB
最終ジャッジ日時 2023-10-31 11:01:59
合計ジャッジ時間 8,097 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 234 ms
35,060 KB
testcase_04 AC 190 ms
32,640 KB
testcase_05 AC 162 ms
35,840 KB
testcase_06 AC 224 ms
37,748 KB
testcase_07 AC 230 ms
33,608 KB
testcase_08 AC 109 ms
35,400 KB
testcase_09 AC 134 ms
35,612 KB
testcase_10 AC 116 ms
37,168 KB
testcase_11 AC 109 ms
35,720 KB
testcase_12 AC 110 ms
35,332 KB
testcase_13 AC 84 ms
31,380 KB
testcase_14 AC 91 ms
38,928 KB
testcase_15 AC 96 ms
35,232 KB
testcase_16 AC 73 ms
31,396 KB
testcase_17 AC 73 ms
33,372 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<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,g[300][300],R[300],C[300];
void solve()
{
	cin >> H >> W;
	long long ans = 0;
	for(int i = 0;i < H;i++)for(int j = 0;j < W;j++) cin >> g[i][j],ans += g[i][j];
	for(int i = 0;i < H;i++) cin >> R[i],ans += R[i];
	for(int j = 0;j < W;j++) cin >> C[j],ans += C[j];
	Graph G(H*W+H+W+2);
	int S = H*W+H+W,T = S+1;
	const long long inf = (long long)1e18;
	for(int i = 0;i < H;i++)for(int j = 0;j < W;j++)
	{
		G.add_edge(S,W*i+j,g[i][j]);
		G.add_edge(W*i+j,H*W+i,inf);
		G.add_edge(W*i+j,H*W+H+j,inf);
	}
	for(int i = 0;i < H;i++)
	{
		long long cost = 0;
		for(int j = 0;j < W;j++) cost += g[i][j];
		G.add_edge(S,H*W+i,R[i]);
		G.add_edge(H*W+i,T,cost);
	}
	for(int j = 0;j < W;j++)
	{
		long long cost = 0;
		for(int i = 0;i < H;i++) cost += g[i][j];
		G.add_edge(S,H*W+H+j,C[j]);
		G.add_edge(H*W+H+j,T,cost);
	}
	Dinic<long long> dc(inf);
	long long mf = dc.maxflow(G,S,T);
	ans -= mf;
	cout << ans << endl;
	/* Graph G(H*W+H+W+2); */
	/* int S = H*W+H+W,T = S+1; */
	/* for(int i = 0;i < H;i++)for(int j = 0;j < W;j++) */
	/* { */
	/* 	int g; */
	/* 	cin >> g; */
	/* 	G.add_edge(W*i+j,T,g); */
	/* } */
	/* long long ans = 0; */
	/* const long long inf = (long long)1e18; */
	/* for(int i = 0;i < H;i++) */
	/* { */
	/* 	int R; */
	/* 	cin >> R; */
	/* 	ans += R; */
	/* 	G.add_edge(S,H*W+i,R); */
	/* 	for(int j = 0;j < W;j++) G.add_edge(H*W+i,W*i+j,inf); */
	/* } */
	/* for(int j = 0;j < W;j++) */
	/* { */
	/* 	int C; */
	/* 	cin >> C; */
	/* 	ans += C; */
	/* 	G.add_edge(S,H*W+H+j,C); */
	/* 	for(int i = 0;i < H;i++) G.add_edge(H*W+H+j,W*i+j,inf); */
	/* } */

	/* Dinic<long long> dc(inf); */
	/* 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