結果

問題 No.957 植林
ユーザー chocoruskchocorusk
提出日時 2019-12-19 22:24:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 766 ms / 2,000 ms
コード長 2,116 bytes
コンパイル時間 1,019 ms
コンパイル使用メモリ 116,260 KB
実行使用メモリ 14,988 KB
最終ジャッジ日時 2023-09-21 07:31:42
合計ジャッジ時間 19,558 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 53 ms
12,424 KB
testcase_04 AC 47 ms
11,824 KB
testcase_05 AC 56 ms
12,620 KB
testcase_06 AC 55 ms
13,156 KB
testcase_07 AC 52 ms
12,844 KB
testcase_08 AC 43 ms
13,376 KB
testcase_09 AC 43 ms
13,072 KB
testcase_10 AC 47 ms
13,972 KB
testcase_11 AC 43 ms
12,900 KB
testcase_12 AC 42 ms
13,580 KB
testcase_13 AC 38 ms
13,120 KB
testcase_14 AC 46 ms
13,872 KB
testcase_15 AC 42 ms
12,624 KB
testcase_16 AC 38 ms
12,492 KB
testcase_17 AC 40 ms
12,064 KB
testcase_18 AC 535 ms
12,560 KB
testcase_19 AC 577 ms
12,976 KB
testcase_20 AC 606 ms
13,800 KB
testcase_21 AC 601 ms
13,176 KB
testcase_22 AC 666 ms
13,948 KB
testcase_23 AC 723 ms
14,696 KB
testcase_24 AC 743 ms
14,204 KB
testcase_25 AC 766 ms
14,436 KB
testcase_26 AC 763 ms
14,808 KB
testcase_27 AC 749 ms
14,740 KB
testcase_28 AC 762 ms
14,440 KB
testcase_29 AC 760 ms
14,460 KB
testcase_30 AC 753 ms
14,436 KB
testcase_31 AC 527 ms
12,756 KB
testcase_32 AC 565 ms
13,172 KB
testcase_33 AC 652 ms
14,056 KB
testcase_34 AC 628 ms
13,280 KB
testcase_35 AC 656 ms
13,776 KB
testcase_36 AC 689 ms
14,032 KB
testcase_37 AC 729 ms
14,252 KB
testcase_38 AC 753 ms
14,472 KB
testcase_39 AC 750 ms
14,728 KB
testcase_40 AC 745 ms
14,988 KB
testcase_41 AC 27 ms
14,540 KB
testcase_42 AC 29 ms
14,764 KB
testcase_43 AC 53 ms
14,592 KB
testcase_44 AC 55 ms
14,412 KB
testcase_45 AC 1 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll INF=1e18;
struct edge {int to; long long int cap; int rev;} ;
 
vector<edge> G[1020];
int level[1020];
int iter[1020];
 
void add_edge(int from, int to, long long int cap){
	edge e;
	e.to=to, e.cap=cap, e.rev=G[to].size();
	G[from].push_back(e);
	e.to=from, e.cap=0, e.rev=G[from].size()-1;
	G[to].push_back(e);
}
 
void bfs(int s){
	memset(level, -1, sizeof(level));
	queue<int> que;
	level[s]=0;
	que.push(s);
	while(!que.empty()){
		int v=que.front();
		que.pop();
		for(int i=0; i<G[v].size(); i++){
			edge e=G[v][i];
			if(e.cap>0 && level[e.to]<0){
				level[e.to]=level[v]+1;
				que.push(e.to);
			}
		}
	}
}
 
long long int dfs(int v, int t, long long int f){
	if(v==t) return f;
	for(int &i=iter[v]; i<G[v].size(); i++){
		edge &e=G[v][i];
		if(e.cap>0 && level[v]<level[e.to]){
			long long int d=dfs(e.to, t, min(f, e.cap));
			if(d>0){
				e.cap-=d;
				G[e.to][e.rev].cap+=d;
				return d;
			}
		}
	}
	return 0;
}
 
long long int max_flow(int s, int t){
	long long int flow=0;
	while(1){
		bfs(s);
		if(level[t]<0) return flow;
		memset(iter, 0, sizeof(iter));
		long long int f;
		while((f=dfs(s, t, INF))>0){
			flow+=f;
		}
	}
}
int main()
{
	int h, w;
  cin>>h>>w;
  int s=h+w, t=s+1;
  ll sum=0;
  for(int i=0; i<h; i++){
    for(int j=0; j<w; j++){
      ll x; cin>>x;
      add_edge(i, t, x);
      add_edge(j+h, i, x);
    }
  }
  for(int i=0; i<h; i++){
    ll x; cin>>x;
    sum+=x;
    add_edge(s, i, x);
  }
  for(int i=0; i<w; i++){
    ll x; cin>>x;
    sum+=x;
    add_edge(s, i+h, x);
  }
  cout<<sum-max_flow(s, t)<<endl;
	return 0;
}
0