結果

問題 No.957 植林
ユーザー beetbeet
提出日時 2019-12-20 23:19:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 745 ms / 2,000 ms
コード長 2,685 bytes
コンパイル時間 2,413 ms
コンパイル使用メモリ 211,608 KB
実行使用メモリ 14,916 KB
最終ジャッジ日時 2023-09-25 01:43:55
合計ジャッジ時間 20,754 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 52 ms
13,400 KB
testcase_04 AC 49 ms
12,008 KB
testcase_05 AC 52 ms
12,468 KB
testcase_06 AC 60 ms
13,772 KB
testcase_07 AC 51 ms
13,192 KB
testcase_08 AC 42 ms
12,676 KB
testcase_09 AC 41 ms
12,408 KB
testcase_10 AC 46 ms
14,432 KB
testcase_11 AC 43 ms
12,412 KB
testcase_12 AC 43 ms
13,436 KB
testcase_13 AC 36 ms
12,104 KB
testcase_14 AC 45 ms
13,416 KB
testcase_15 AC 40 ms
12,612 KB
testcase_16 AC 36 ms
12,184 KB
testcase_17 AC 37 ms
11,896 KB
testcase_18 AC 540 ms
12,344 KB
testcase_19 AC 556 ms
12,728 KB
testcase_20 AC 574 ms
13,328 KB
testcase_21 AC 608 ms
13,532 KB
testcase_22 AC 636 ms
13,940 KB
testcase_23 AC 659 ms
13,804 KB
testcase_24 AC 681 ms
14,916 KB
testcase_25 AC 733 ms
14,272 KB
testcase_26 AC 726 ms
14,580 KB
testcase_27 AC 741 ms
14,200 KB
testcase_28 AC 718 ms
14,360 KB
testcase_29 AC 718 ms
14,584 KB
testcase_30 AC 721 ms
14,400 KB
testcase_31 AC 533 ms
12,416 KB
testcase_32 AC 558 ms
13,656 KB
testcase_33 AC 575 ms
13,012 KB
testcase_34 AC 606 ms
13,108 KB
testcase_35 AC 637 ms
14,200 KB
testcase_36 AC 663 ms
14,068 KB
testcase_37 AC 691 ms
14,276 KB
testcase_38 AC 745 ms
14,072 KB
testcase_39 AC 725 ms
14,252 KB
testcase_40 AC 726 ms
14,588 KB
testcase_41 AC 27 ms
14,348 KB
testcase_42 AC 28 ms
14,204 KB
testcase_43 AC 51 ms
14,140 KB
testcase_44 AC 57 ms
14,200 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T,bool directed>
struct Dinic{
  struct edge {
    int to;
    T cap;
    int rev;
    edge(){}
    edge(int to,T cap,int rev):to(to),cap(cap),rev(rev){}
  };

  vector<vector<edge> > G;
  vector<int> level,iter;

  Dinic(){}
  Dinic(int n):G(n),level(n),iter(n){}

  int add_edge(int from,int to,T cap){
    G[from].emplace_back(to,cap,G[to].size());
    G[to].emplace_back(from,directed?0:cap,G[from].size()-1);
    return G[to].back().rev;
  }

  void bfs(int s){
    fill(level.begin(),level.end(),-1);
    queue<int> que;
    level[s]=0;
    que.emplace(s);
    while(!que.empty()){
      int v=que.front();que.pop();
      for(int i=0;i<(int)G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap>0&&level[e.to]<0){
          level[e.to]=level[v]+1;
          que.emplace(e.to);
        }
      }
    }
  }

  T dfs(int v,int t,T f){
    if(v==t) return f;
    for(int &i=iter[v];i<(int)G[v].size();i++){
      edge &e=G[v][i];
      if(e.cap>0&&level[v]<level[e.to]){
        T d=dfs(e.to,t,min(f,e.cap));
        if(d==0) continue;
        e.cap-=d;
        G[e.to][e.rev].cap+=d;
        return d;
      }
    }
    return 0;
  }

  T flow(int s,int t,T lim){
    T fl=0;
    while(1){
      bfs(s);
      if(level[t]<0||lim==0) break;
      fill(iter.begin(),iter.end(),0);

      while(1){
        T f=dfs(s,t,lim);
        if(f==0) break;
        fl+=f;
        lim-=f;
      }
    }
    return fl;
  }

  T flow(int s,int t){
    return flow(s,t,numeric_limits<T>::max()/2);
  }

  T cut(int s,int t,int x,int a){
    static_assert(directed, "must be directed");
    auto &e=G[x][a];
    int y=e.to;
    T cr=G[y][e.rev].cap;
    if(cr==0) return e.cap=0;
    e.cap=G[y][e.rev].cap=0;
    T cap=cr-flow(x,y,cr);
    if(x!=s&&cap!=0) flow(x,s,cap);
    if(t!=y&&cap!=0) flow(t,y,cap);
    return cap;
  }

  T link(int s,int t,int x,int a,T f){
    auto &e=G[x][a];
    e.cap+=f;
    return flow(s,t,f);
  }
};

//INSERT ABOVE HERE
signed main(){
  int h,w;
  cin>>h>>w;

  using ll = long long;
  Dinic<ll, true> G(h+w+2);
  int S=h+w;
  int T=S+1;

  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      int g;
      cin>>g;
      G.add_edge(i,h+j,g);
      G.add_edge(S,i,g);
    }
  }

  ll sum=0;
  for(int i=0;i<h;i++){
    int r;
    cin>>r;
    sum+=r;
    G.add_edge(i,T,r);
  }

  for(int j=0;j<w;j++){
    int c;
    cin>>c;
    sum+=c;
    G.add_edge(h+j,T,c);
  }

  cout<<sum-G.flow(S,T)<<endl;
  return 0;
}
0