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
140
141
142
143
144
145
| #include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 5000,MAXM = 500000;
struct Edge{
int from,to;
int cap,flow;
int cost,nex;
}edge[MAXM];
int n,m,s,t;
int fir[MAXN],ecnt = 2,maxf = 0;
int dis[MAXN],instack[MAXN],pree[MAXN];
int b[25][25],e[25][25],num[25][25];
queue<int> q;
int tr(int a,int b){
if(a == 0 || b == 0 || a>n||b>m)
return -1;
return (a-1)*m+b;
}
void addedge(int a,int b,int c,int d){
if(a <= 0 || b <= 0 || c == 0) return;
edge[ecnt] = (Edge){a,b,c,0,d,fir[a]};
fir[a] = ecnt++;
edge[ecnt] = (Edge){b,a,0,0,-d,fir[b]};
fir[b] = ecnt++;
}
bool spfa(){
memset(dis,0x3f,sizeof(dis));
memset(instack,0,sizeof(instack));
while(!q.empty()) q.pop();
dis[s] = 0;q.push(s);
while(!q.empty()){
int nown = q.front();q.pop();
instack[nown] = 0;
for(int nowe = fir[nown];nowe;nowe = edge[nowe].nex){
Edge e = edge[nowe];
if(dis[e.to] > dis[nown] + e.cost && e.cap > e.flow){
dis[e.to] = dis[nown] + e.cost;
pree[e.to] = nowe;
if(instack[e.to] == 0){
q.push(e.to);
instack[e.to] = 1;
}
}
}
}
return dis[t] < 0x3f3f3f3f;
}
void argument(int &sumf,int &sumc){
int nown = t,limit = 0x3f3f3f3f,nowe;
while(nown!=s){
nowe = pree[nown];
limit = min(limit,edge[nowe].cap - edge[nowe].flow);
nown = edge[nowe].from;
}
nown = t;
while(nown!=s){
nowe = pree[nown];
edge[nowe].flow += limit,edge[nowe^1].flow -= limit;
nown = edge[nowe].from;
}
sumf += limit,sumc += limit * dis[t];
}
void init(){
scanf("%d %d",&n,&m);s = 1,t = 2;
char tmp[50];
for(int i = 1;i<=n;i++){
scanf("%s",tmp);
for(int j = 1;j<=m;j++)
b[i][j] = tmp[j-1]^48;
}
for(int i = 1;i<=n;i++){
scanf("%s",tmp);
for(int j = 1;j<=m;j++)
e[i][j] = tmp[j-1]^48;
}
for(int i = 1;i<=n;i++){
scanf("%s",tmp);
for(int j = 1;j<=m;j++)
num[i][j] = tmp[j-1]^48;
}
}
void build(){
int bb = 0,ee = 0;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
int tmp = tr(i,j);
if(b[i][j] && !e[i][j]){
num[i][j]-=1;
addedge(3*tmp+1,3*tmp+3,num[i][j]/2,0);
addedge(3*tmp+3,3*tmp+2,num[i][j]/2+1,0);
addedge(s,3*tmp+3,1,0);
maxf = max(maxf,++bb);
}
else if(e[i][j] && !b[i][j]){
num[i][j]-=1;
addedge(3*tmp+1,3*tmp+3,num[i][j]/2+1,0);
addedge(3*tmp+3,3*tmp+2,num[i][j]/2,0);
addedge(3*tmp+3,t,1,0);
maxf = max(maxf,++ee);
}
else{
addedge(3*tmp+1,3*tmp+3,num[i][j]/2,0);
addedge(3*tmp+3,3*tmp+2,num[i][j]/2,0);
}
addedge(3*tmp+2,3*tr(i-1,j-1)+1,100000,1);
addedge(3*tmp+2,3*tr(i+1,j+1)+1,100000,1);
addedge(3*tmp+2,3*tr(i+1,j-1)+1,100000,1);
addedge(3*tmp+2,3*tr(i-1,j+1)+1,100000,1);
addedge(3*tmp+2,3*tr(i,j-1)+1,100000,1);
addedge(3*tmp+2,3*tr(i,j+1)+1,100000,1);
addedge(3*tmp+2,3*tr(i+1,j)+1,100000,1);
addedge(3*tmp+2,3*tr(i-1,j)+1,100000,1);
}
}
}
void solve(){
int f = 0,c = 0;
while(spfa())
argument(f,c);
if(f!=maxf)
printf("-1");
else
printf("%d\n",c);
}
int main(){
init();
build();
solve();
return 0;
}
|