結果

問題 No.957 植林
ユーザー treeone
提出日時 2019-12-19 15:22:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,860 bytes
コンパイル時間 2,403 ms
コンパイル使用メモリ 200,536 KB
最終ジャッジ日時 2025-01-08 12:44:59
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 5 WA * 2 TLE * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
using ll = long long;
const ll INF = 1e16;
struct edge{
int to;
ll cap;
int rev;
};
vector<edge> G[610];
bool used[610];
void add_edge(int from, int to, ll cap){
G[from].push_back({to, cap, (int)G[to].size()});
G[to].push_back({from, 0LL, (int)G[from].size() - 1});
}
ll dfs(int v, int t, ll f){
if(v == t) return f;
used[v] = true;
for(int i = 0; i < G[v].size(); i++){
edge &e = G[v][i];
if(!used[e.to] && e.cap > 0){
ll 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;
}
ll max_flow(int s, int t){
ll flow = 0;
for(;;){
memset(used, 0, sizeof(used));
ll f = dfs(s, t, INF);
if(f == 0) return flow;
flow += f;
}
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int H, W;
cin >> H >> W;
vector<vector<ll> > G(H, vector<ll>(W));
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
cin >> G[i][j];
}
}
vector<ll> R(H), C(W), S(H);
int ans = 0;
for(int i = 0; i < H; i++){
cin >> R[i];
for(int j = 0; j < W; j++){
S[i] += G[i][j];
}
ans += max(R[i] - S[i], 0LL);
}
for(int i = 0; i < W; i++){
cin >> C[i];
ans += C[i];
}
for(int i = 0; i < H; i++){
add_edge(0, i + 1, max(S[i] - R[i], 0LL));
}
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
add_edge(i + 1, H + j + 1, G[i][j]);
}
}
for(int i = 0; i < W; i++){
add_edge(H + i + 1, H + W + 1, C[i]);
}
cout << ans - max_flow(0, H + W + 1) << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0