結果
問題 |
No.957 植林
|
ユーザー |
![]() |
提出日時 | 2019-12-20 00:01:56 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,109 bytes |
コンパイル時間 | 2,714 ms |
コンパイル使用メモリ | 210,988 KB |
最終ジャッジ日時 | 2025-01-08 13:01:54 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 3 |
other | WA * 44 TLE * 1 |
ソースコード
#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 TF,typename TC> struct PrimalDual{ struct edge{ int to; TF cap; TC cost; int rev; edge(){} edge(int to,TF cap,TC cost,int rev): to(to),cap(cap),cost(cost),rev(rev){} }; static const TC INF; vector<vector<edge>> G; vector<TC> h,dist; vector<int> prevv,preve; PrimalDual(){} PrimalDual(int n):G(n),h(n),dist(n),prevv(n),preve(n){} void add_edge(int u,int v,TF cap,TC cost){ G[u].emplace_back(v,cap,cost,G[v].size()); G[v].emplace_back(u,0,-cost,G[u].size()-1); } void dijkstra(int s){ struct P{ TC first; int second; P(TC first,int second):first(first),second(second){} bool operator<(const P&a) const{return a.first<first;} }; priority_queue<P> que; fill(dist.begin(),dist.end(),INF); dist[s]=0; que.emplace(dist[s],s); while(!que.empty()){ P p=que.top();que.pop(); int v=p.second; if(dist[v]<p.first) continue; for(int i=0;i<(int)G[v].size();i++){ edge &e=G[v][i]; if(e.cap==0) continue; if(dist[v]+e.cost+h[v]-h[e.to]<dist[e.to]){ dist[e.to]=dist[v]+e.cost+h[v]-h[e.to]; prevv[e.to]=v; preve[e.to]=i; que.emplace(dist[e.to],e.to); } } } } TC flow(int s,int t,TF f,int &ok){ TC res=0; fill(h.begin(),h.end(),0); while(f>0){ dijkstra(s); if(dist[t]==INF){ ok=0; return res; } for(int v=0;v<(int)h.size();v++) if(dist[v]<INF) h[v]=h[v]+dist[v]; TF d=f; for(int v=t;v!=s;v=prevv[v]) d=min(d,G[prevv[v]][preve[v]].cap); f-=d; res=res+h[t]*d; for(int v=t;v!=s;v=prevv[v]){ edge &e=G[prevv[v]][preve[v]]; e.cap-=d; G[v][e.rev].cap+=d; } } ok=1; return res; } }; template<typename TF, typename TC> const TC PrimalDual<TF, TC>::INF = numeric_limits<TC>::max()/2; template<typename TF,typename TC> struct NegativeEdge{ PrimalDual<TF, TC> G; vector<TF> fs; TC sum; int S,T; NegativeEdge(){} NegativeEdge(int n):G(n+2),fs(n+2,0),sum(0),S(n),T(n+1){} void use_edge(int u,int v,TF cap,TC cost){ fs[u]-=cap; fs[v]+=cap; sum=sum+cost*cap; } void add_edge(int u,int v,TF cap,TC cost){ if(cost<TC(0)){ use_edge(u,v,cap,cost); swap(u,v); cost=-cost; } G.add_edge(u,v,cap,cost); } TC flow(int &ok){ TF f=0; for(int i=0;i<S;i++){ if(fs[i]>0){ f+=fs[i]; G.add_edge(S,i,+fs[i],TC(0)); } if(fs[i]<0){ G.add_edge(i,T,-fs[i],TC(0)); } } return sum+G.flow(S,T,f,ok); } TC flow(int ts,int tt,TF tf,int &ok){ fs[ts]+=tf; fs[tt]-=tf; return flow(ok); } }; //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]; using ll = long long; NegativeEdge<ll, ll> F(h+w+3); int S=h+w; int T=S+1; int Z=T+1; ll sr=0,sc=0; for(int i=0;i<h;i++) sr+=rs[i]; for(int j=0;j<w;j++) sc+=cs[j]; const ll CAP = max(sr,sc) * 10; const ll INF = 1e9; ll sum=0; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ sum-=G[i][j]; sum+=G[i][j]*INF; F.add_edge(i,h+j,G[i][j],-INF); F.add_edge(i,h+j,CAP,0); } } for(int i=0;i<h;i++){ sum+=rs[i]; F.add_edge(S,i,rs[i],0); F.add_edge(Z,i,CAP,1); F.add_edge(i,Z,CAP,0); } for(int j=0;j<w;j++){ F.add_edge(h+j,T,cs[j],0); F.add_edge(Z,h+j,CAP,1); F.add_edge(h+j,Z,CAP,0); } if(sr<sc) F.add_edge(S,Z,sc-sr,0); if(sr>sc) F.add_edge(Z,T,sr-sc,0); cout<<max(sr,sc)<<endl; int ok; cout<<sum+F.flow(S,T,max(sr,sc),ok)<<endl; return 0; }