考虑把S设为文科,T设为理科,
S到每个点连边,流量为选文HAPPY+1/2(和四周一起选文的贡献)
T理科同理。
把四周挨着的建双向边条,流量为1/2(两人同选文加同选理的贡献)即可。
读入太扯淡了
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<queue> #define inf 100000000 using namespace std; int w_[105][105],l_[105][105],w_h[105][105],w_z[105][105],l_h[105][105],l_z[105][105]; int n,m,S=0,T,cnt,a[105][105],sum,ans; int adj[10005],e=2,dep[10005]; struct node { int v,next,l; } lu[100004]; int read() { int s=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0') {if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {s=(s<<1)+(s<<3)+(ch^48);ch=getchar();} return s*f; } void add(int u,int v,int l) {lu[e].v=v;lu[e].l=l;lu[e].next=adj[u];adj[u]=e++;} int bfs() { queue<int> q; memset(dep,0,sizeof(dep)); q.push(S); dep[S]=1; while(!q.empty()) { int x=q.front();q.pop(); for(int i=adj[x];i;i=lu[i].next) { if(lu[i].l&&!dep[lu[i].v]) {//cout<<lu[i].l<<" "; dep[lu[i].v]=dep[x]+1; q.push(lu[i].v); if(lu[i].v==T) return 1; } } } return 0; } int dfs(int x,int len) { if(x==T)return len; int tmp=len,k; for(int i=adj[x];i;i=lu[i].next) { int to=lu[i].v; if(tmp&&dep[to]==dep[x]+1&&lu[i].l) { k=dfs(to,min(lu[i].l,tmp)); if(!k) { dep[to]=0; continue; } lu[i].l-=k; lu[i^1].l+=k; tmp-=k; } } return len-tmp; } int yjn() { freopen("nt2011_happiness.in","r",stdin); freopen("nt2011_happiness.out","w",stdout); scanf("%d%d",&n,&m);T=n*m+1; for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)w_ [i][j]=read(),sum+=w_ [i][j]; for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)l_ [i][j]=read(),sum+=l_ [i][j]; for(int i=1;i< n;i++)for(int j=1;j<=m;j++)w_z[i][j]=read(),sum+=w_z[i][j]; for(int i=1;i< n;i++)for(int j=1;j<=m;j++)l_z[i][j]=read(),sum+=l_z[i][j]; for(int i=1;i<=n;i++)for(int j=1;j< m;j++)w_h[i][j]=read(),sum+=w_h[i][j]; for(int i=1;i<=n;i++)for(int j=1;j< m;j++)l_h[i][j]=read(),sum+=l_h[i][j]; for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)a[i][j]=++cnt; for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) { add(S,a[i][j],2*w_[i][j]+w_z[i][j]+w_z[i-1][j]+w_h[i][j]+w_h[i][j-1]);add(a[i][j],S,0); add(a[i][j],T,2*l_[i][j]+l_z[i][j]+l_z[i-1][j]+l_h[i][j]+l_h[i][j-1]);add(T,a[i][j],0); if(i!=n){add(a[i][j],a[i+1][j],w_z[i][ j ]+l_z[i][ j ]),add(a[i+1][j],a[i][j],w_z[i][ j ]+l_z[i][ j ]);} if(j!=m){add(a[i][j],a[i][j+1],w_h[i][ j ]+l_h[i][ j ]),add(a[i][j+1],a[i][j],w_h[i][ j ]+l_h[i][ j ]);} } while(bfs()) ans+=dfs(S,inf); cout<<(sum*2-ans)/2; } int qty=yjn(); int main(){;}