結果

問題 No.957 植林
ユーザー beetbeet
提出日時 2019-12-19 23:02:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,016 bytes
コンパイル時間 2,646 ms
コンパイル使用メモリ 215,200 KB
実行使用メモリ 17,780 KB
最終ジャッジ日時 2023-09-21 07:37:25
合計ジャッジ時間 6,741 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 47 ms
16,232 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 47 ms
15,740 KB
testcase_09 AC 47 ms
16,480 KB
testcase_10 AC 49 ms
16,436 KB
testcase_11 AC 47 ms
16,300 KB
testcase_12 WA -
testcase_13 AC 42 ms
15,100 KB
testcase_14 AC 52 ms
16,684 KB
testcase_15 AC 46 ms
15,752 KB
testcase_16 AC 42 ms
15,496 KB
testcase_17 AC 43 ms
14,980 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 36 ms
17,164 KB
testcase_42 AC 36 ms
17,232 KB
testcase_43 AC 61 ms
17,024 KB
testcase_44 AC 64 ms
17,580 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 1 ms
4,376 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;
  vector< vector<int> > G(h,vector<int>(w));
  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      cin>>G[i][j];

  vector<int> rs(h),cs(w);
  for(int i=0;i<h;i++) cin>>rs[i];
  for(int j=0;j<w;j++) cin>>cs[j];

  Dinic<long long, true> F(h*w+h+w+3);
  int S=h*w+h+w;
  int T=S+1;
  int U=T+1;

  long long sum=0;
  for(int r:rs) sum+=r;
  for(int c:cs) sum+=c;

  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      F.add_edge(S,i*w+j,G[i][j]);

  const long long INF = 1e15;
  for(int i=0;i<h;i++){
    F.add_edge(U,h*w+i,INF);
    F.add_edge(h*w+i,T,rs[i]);
  }

  for(int j=0;j<w;j++){
    F.add_edge(U,h*w+h+j,INF);
    F.add_edge(h*w+h+j,T,cs[j]);
  }

  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      F.add_edge(i*w+j,U,INF);

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