1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=505; const int inf=0x3f3f3f3f; struct edge { int to,nxt; int c; }e[2*305*305]; int cnt=1,head[N]; int s,t; int d[N],cur[N]; void add(int u,int v,int c) { e[++cnt]={v,head[u],c};head[u]=cnt; e[++cnt]={u,head[v],0};head[v]=cnt; } bool bfs() { memset(d,0,sizeof(d)); queue<int> q; d[s]=1;q.push(s); while(!q.empty()) { int u=q.front();q.pop(); for(int i=head[u];i;i=e[i].nxt) { int v=e[i].to; if(d[v]==0&&e[i].c) { d[v]=d[u]+1;q.push(v); if(v==t) return true; } } } return false; } int dfs(int u,int mf) { if(u==t) return mf; int sum=0; for(int i=cur[u];i;i=e[i].nxt) { cur[u]=i; int v=e[i].to; if(d[v]==d[u]+1&&e[i].c) { int res=dfs(v,min(mf,e[i].c)); e[i].c-=res; e[i^1].c+=res; sum+=res; mf-=res; if(mf==0) break; } } if(sum==0) d[u]=0; return sum; } int dinic() { int flow=0; while(bfs()) { memcpy(cur,head,sizeof(cur)); flow+=dfs(s,inf); } return flow; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n,m,c,d;cin>>n>>m>>c>>d; s=0,t=n+m+1; for(int i=1;i<=n;i++) add(s,i,0); for(int i=1;i<=m;i++) add(i+n,t,0); int z=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { char ch;cin>>ch; if(ch=='.') { z++; add(i,j+n,1); } } ll ans=1ll*d*z; for(int k=1;k<=max(n,m);k++) { for(int i=2;i<=2*n;i+=2) e[i].c++; for(int i=2*n+2;i<=2*(n+m);i+=2) e[i].c++; z-=dinic(); ans=min(ans,1ll*c*k+1ll*d*z); } cout<<ans<<'\n'; return 0; }
|