//由于给定的点最高可达100*100=10000 //因此不可能建立一个10000*10000的图来描述流量,必定MLE //但考虑到是格子形状,每一个最多只与周边4个点相连 //可以通过这个性质进行图的压缩,并建立一个方向和点的转化关系 //再套用最大流模板便可通过 #include<iostream> #include<queue> #include<cstring> using namespace std; const int MAX = 102,INF = 2000000000; int flow[MAX*MAX][4],cap[MAX*MAX][4],rflow[MAX*MAX]; int pre[MAX*MAX]; int c,maxflow; int st,ed,t,n,m; inline int Dir(int u,int v)//通过两点的关系转化返回方向 { if(v - u == 1) return 0; if(u - v == 1) return 2; if(v - m == u) return 1; if(v + m == u) return 3; } inline int trans(int u,int v)//通过点和方向的关系返回坐标 { if(v == 0) return u + 1; if(v == 1) return u + m; if(v == 2) return u - 1; if(v == 3) return u - m; } int main() { //freopen("in.txt","r",stdin); scanf("%d",&t); while(t--) { memset(flow,0,sizeof(flow)); memset(cap,0,sizeof(cap)); scanf("%d%d",&n,&m); st = 1; ed = n*m; for(int i = 1;i <= n;++i) { for(int j = 1;j <= m - 1;++j) { scanf("%d",&c); cap[j + (i-1)*m][0] = cap[j + (i-1)*m + 1][2] = c; } } for(int i = 1;i <= n - 1;++i) { for(int j = 1;j <= m;++j) { scanf("%d",&c); cap[(i-1)*m + j][1] = cap[i * m + j][3] = c; } } maxflow = 0; for(;;)//最大流模板 { queue<int> q; memset(rflow,0,sizeof(rflow)); rflow[st] = INF; q.push(st); while(!q.empty()) { int u = q.front(); q.pop(); for(int v = 0;v < 4;++v) { if(!rflow[trans(u,v)] && cap[u][v] > flow[u][v]) { q.push(trans(u,v)); pre[trans(u,v)] = u; rflow[trans(u,v)] = min(rflow[u],cap[u][v] - flow[u][v]); } } } if(rflow[ed] == 0) break; for(int u = ed; u != st;u = pre[u]) { flow[pre[u]][Dir(pre[u],u)] += rflow[ed]; flow[u][Dir(u,pre[u])] -= rflow[ed]; } maxflow += rflow[ed]; } printf("%d/n",maxflow); } return 0; }