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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ std::cout<<x; } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=50,MAXV=1e4+10,MAXE=1e5+10; const int INF=0x3f3f3f3f; int N,M,a,b,c,S,T,Idx; struct Net { int next,to,val; }Edge[MAXE<<1]; int Head[MAXV],Cur[MAXV],Total=1; inline void addEdge(int u,int v,int w) { Edge[++Total]=(Net){Head[u],v,w};Head[u]=Total; Edge[++Total]=(Net){Head[v],u,0};Head[v]=Total; } int Fl[MAXV]; inline bool Bfs() { std::queue<int>Q; std::memset(Fl,-1,sizeof(Fl)); Fl[S]=1,Cur[S]=Head[S],Q.push(S); while(!Q.empty()) { int u=Q.front();Q.pop(); for(int e=Head[u],v;e;e=Edge[e].next) { v=Edge[e].to; if(Fl[v]==-1&&Edge[e].val) { Fl[v]=Fl[u]+1,Cur[v]=Head[v]; if(v==T) return 1; Q.push(v); } } } return 0; } int Dfs(int x,int inf) { if(x==T) return inf; int flow=0; for(int e=Cur[x],v;e&&flow<inf;e=Edge[e].next) { v=Edge[e].to,Cur[x]=e; if(Fl[v]==Fl[x]+1&&Edge[e].val) { int k=Dfs(v,std::min(Edge[e].val,inf-flow)); if(!k) Fl[v]=-1; flow+=k,Edge[e].val-=k,Edge[e^1].val+=k; } } return flow; } inline int Dinic() { int r=0,flow; while(Bfs()) while(flow=Dfs(S,INF)) r+=flow; return r; } char Str[MAXN][MAXN]; int Br[MAXN][MAXN],Bc[MAXN][MAXN],Wr[MAXN][MAXN],Wc[MAXN][MAXN]; int main() { read(N,M,a,b,c); for(int i=1;i<=N;++i) scanf("%s",Str[i]+1); for(int i=1;i<=N;++i) for(int j=1;j<=M;++j) { Br[i][j]=++Idx,Bc[i][j]=++Idx, Wr[i][j]=++Idx,Wc[i][j]=++Idx; } S=++Idx,T=++Idx; for(int i=1;i<=N;++i) for(int j=1;j<=M;++j) { addEdge(S,Br[i][j],a),addEdge(S,Wc[i][j],a); addEdge(Bc[i][j],T,a),addEdge(Wr[i][j],T,a); addEdge(Wr[i][j],Br[i][j],INF),addEdge(Bc[i][j],Wc[i][j],INF); if(Str[i][j]=='#') { addEdge(Wr[i][j],T,INF),addEdge(S,Wc[i][j],INF); addEdge(Br[i][j],Bc[i][j],c); } else { addEdge(Bc[i][j],Br[i][j],INF); addEdge(Wc[i][j],Br[i][j],c),addEdge(Bc[i][j],Wr[i][j],c); } if(j==1) addEdge(S,Br[i][j],b),addEdge(Wr[i][j],T,b); else addEdge(Br[i][j-1],Br[i][j],b),addEdge(Wr[i][j],Wr[i][j-1],b); if(i==1) addEdge(Bc[i][j],T,b),addEdge(S,Wc[i][j],b); else addEdge(Bc[i][j],Bc[i-1][j],b),addEdge(Wc[i-1][j],Wc[i][j],b); } write(Dinic()); return 0; }
|