这题真是做死我了,调了一个早上。。
然后还没用弄出一个正确的图QAQ
于是下午时突发奇想,又想到了一些以为很对的东西,于是又搞错了
ORZ
感觉思路大概都对。。
注意时大概。。
但是还是,怎么说,思路还是有一点点混乱吧。。
总结一下问题:没有一个清晰的方向,就是有一个问题就补一个,于是就永远补不完了。。
看见这个问题,就拆点。。
那个问题又限制了流量。。
于是问题就一个接一个。。
╮(╯▽╰)╭
下次一定要理清思路再搞,不可以盲目地瞎搞。。
贴一个我的WA代码【费了很多心血的】
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstring>
using namespace std;
const int N=25*25*4;
const int MAX=1<<30;
int n,m;
char ss[N];//起始状态 终止状态 可以移多少次
char k[N][N];
int st,ed;
int X[8]={-1,0,1,-1,1,-1,0,1};
int Y[8]={-1,-1,-1,0,0,1,1,1};
int cnt=0,cnt1=0;
struct qq
{
int x,y,z,z1,last;//流量 费用
}s[25*25*30*2];
bool ok[25][25];
bool ok1[25][25];
int num,last[N];
void init (int x,int y,int z,int z1)
{
num++;
s[num].x=x;s[num].y=y;s[num].z=z;s[num].z1=z1;
s[num].last=last[x];
last[x]=num;
swap(x,y);z=0;z1=-z1;
num++;
s[num].x=x;s[num].y=y;s[num].z=z;s[num].z1=z1;
s[num].last=last[x];
last[x]=num;
}
int from[N];
int f[N];
bool in[N];
bool SPFA ()
{
memset(in,false,sizeof(in));
memset(from,-1,sizeof(from));
memset(f,127,sizeof(f));
queue<int> q;
q.push(st);
f[st]=0;in[st]=true;
while (!q.empty())
{
int x=q.front();q.pop();
//printf("%d ",x);
for (int u=last[x];u!=-1;u=s[u].last)
{
int y=s[u].y;
/*printf("NO:%d %d\n",y,u);
system("pause");*/
if (s[u].z>0&&f[y]>f[x]+s[u].z1)
{
f[y]=f[x]+s[u].z1;
from[y]=u;
if (in[y]==false)
{
in[y]=true;
q.push(y);
}
}
}
in[x]=false;
}
// printf("YES");
return from[ed]!=-1;
}
int mymin (int x,int y)
{
return x<y?x:y;
}
int ans=0;
void lalal ()
{
int x=from[ed],t=MAX;
// printf("%d ",ed);
while (x!=-1)
{
t=mymin(t,s[x].z);
// printf("%d ",s[x].x);
x=from[s[x].x];
}
// printf("\n");
x=from[ed];
while (x!=-1)
{
ans=ans+t*s[x].z1;
s[x].z-=t;
s[x^1].z+=t;
// printf("%d ",ans);
x=from[s[x].x];
}
// printf("\n");
return ;
}
int P (int x,int y){return (x-1)*m+y;}
bool check ()//是不是满流
{
for (int u=last[ed];u!=-1;u=s[u].last)
{
// printf("YES:%d %d %d %d\n",s[u].x,s[u].y,s[u].z,s[u].z1);
if (s[u].z<=0) return false;
}
return true;
}
int main()
{
num=1;
memset(ok,false,sizeof(ok));
memset(ok1,false,sizeof(ok1));
memset(last,-1,sizeof(last));
scanf("%d%d",&n,&m);
st=n*m*3+1;ed=st+1;
for (int u=1;u<=n;u++)
scanf("%s",k[u]+1);
for (int u=1;u<=n;u++)
{
scanf("%s",ss+1);
for (int i=1;i<=m;i++)
if (ss[i]=='1')
{
if (k[u][i]=='1') {k[u][i]='0';continue;}
ok[u][i]=true;
cnt1++;
init(P(u,i),ed,1,0);
}
}
for (int u=1;u<=n;u++)
for (int i=1;i<=m;i++)
if (k[u][i]=='1')
{
cnt++;
init(st,P(u,i),1,0);
}
if (cnt!=cnt1) {printf("-1\n");return 0;}
for (int u=1;u<=n;u++)
{
scanf("%s",ss+1);
for (int i=1;i<=m;i++)
{
int x=ss[i]-'0';
init(P(u,i)+n*m,P(u,i),(x+1)/2,0);
init(P(u,i),P(u,i)+n*m*2,(x+1)/2,0);
for (int j=0;j<8;j++)
{
int tx=u+X[j],ty=i+Y[j];
if (tx<=0||tx>n||ty<=0||ty>m) continue;
init(P(u,i)+n*m*2,P(tx,ty)+n*m,MAX,1);
}
}
}
while (SPFA()==true)
{
lalal();
}
if (check()==true) printf("%d\n",ans);
else printf("-1");
return 0;
}