然后我们可以机智的SPFA就过啦2333333333,
主要就是最开始交无限RE,然后看到了别人说用读入优化会错的
卧槽!!然后改成scanf,交,还是RE= =
然后我去看了下hzwer的程序,发现我们似乎在输出上面有点不同
改成printf,A了= =,然后用回读入优化,还是A了= =
23333333333333333333333333333333333333333333333333【无情的嘲笑一下自己的傻逼
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define efo(i,x) for(int i=last[x];i!=0;i=e[i].next)
using namespace std;
inline LL read()
{
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if (s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
#define M 2000005
struct edge
{
int y,w,next;
}e[M*3];
int last[M],ne=0;
int n,m,mn;
void add(int x,int y,int w)
{
e[++ne].y=y;e[ne].w=w;e[ne].next=last[x];last[x]=ne;
}
void add2(int u,int v,int w)
{
add(u,v,w);add(v,u,w);
// cout<<u<<' '<<v<<' '<<w<<endl;
}
int dis[M],q[M*9];
bool f[M]={0};
int getans()
{
int h=0,t=1;
q[1]=0;
memset(dis,127,sizeof(dis));
dis[0]=0;f[0]=1;
while (h<t)
{
int now=q[++h];
f[now]=0;
// cout<<now<<endl;
efo(i,now)
if (dis[now]+e[i].w<dis[e[i].y])
{
dis[e[i].y]=dis[now]+e[i].w;
if (f[e[i].y]==0)
{
q[++t]=e[i].y;
f[e[i].y]=1;
}
}
}
return dis[mn+1];
}
int main()
{
// memset(last,0,sizeof(last));
n=read(),m=read();
mn=(n-1)*(m-1)*2;
fo(i,1,n)
{
fo(j,1,m-1)
{
int x=read();
if (i==1) add2(j*2,mn+1,x);
else if (i==n) add2(0,(n-2)*(m-1)*2+j*2-1,x);
else add2((i-2)*(m-1)*2+j*2-1,(i-1)*(m-1)*2+j*2,x);
}
}
fo(i,1,n-1)
{
fo(j,1,m)
{
int x=read();
if (j==1) add2(0,(i-1)*(m-1)*2+1,x);
else if (j==m) add2(mn+1,i*(m-1)*2,x);
else add2((i-1)*(m-1)*2+(j-1)*2,(i-1)*(m-1)*2+(j-1)*2+1,x);
}
}
fo(i,1,n-1)
{
fo(j,1,m-1)
{
int x=read();
add2((i-1)*(m-1)*2+j*2-1,(i-1)*(m-1)*2+j*2,x);
}
}
// cout<<last[0]<<' '<<last[1]<<' '<<ne<<endl;
int ans=getans();
// cout<<ans<<endl;
printf("%d",ans);
return 0;
}