记录编号 607144 评测结果 AAAAAAAAAA
题目名称 3113.[BZOJ 4676] Xor-Mul 棋盘 最终得分 100
用户昵称 Gravatar梦那边的美好TE 是否通过 通过
代码语言 C++ 运行时间 7.736 s
提交时间 2025-10-07 11:01:50 内存使用 5.67 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int N=7,M=10005;
const int V=(1<<5);
const ll inf=1e15;
ll dp[M][V],ans;
int n,m,a[N][M],b[N][M],c[N][M],d[N][M];
ll count(int S,int r,int k){
    ll res=0;
    for(int j=0;j<n;j++){
        if(((S>>j)&1)^((a[j+1][r]>>k)&1)){
            res+=b[j+1][r];
        }
    }
    for(int j=0;j<n-1;j++){
        if(((S>>j)&1)^((S>>(j+1))&1)){
            res+=d[j+1][r];
        }
    }
    if((S>>(n-1)&1)^(S&1))res+=d[n][r];
    return res;
}
ll value(int S,int T,int r){
	ll res=0;
	for(int j=0;j<n;j++){
		if(((S>>j)&1)^((T>>j)&1)){
			res+=c[j+1][r];
		}
	} 
	return res;
} 
ll calc(int k){
	ll res=inf;
    for(int S=0;S<(1<<n);S++)dp[1][S]=count(S,1,k);
    for(int i=2;i<=m;i++){
        for(int S=0;S<(1<<n);S++){
            dp[i][S]=count(S,i,k);
            ll tmp=inf;
            for(int T=0;T<(1<<n);T++){
                tmp=min(tmp,value(T,S,i-1)+dp[i-1][T]);
            }
            dp[i][S]+=tmp;
            if(i==m)res=min(res,dp[i][S]);
        }
    }
    return res;
}
int main(){
	freopen("chessboardd.in","r",stdin);
	freopen("chessboardd.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&b[i][j]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<m;j++){
            scanf("%d",&c[i][j]);
        }
    }
    for(int i=1;i<n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&d[i][j]);
        }
    }
    for(int j=1;j<=m;j++){
        scanf("%d",&d[n][j]);
    }
    for(int i=0;i<=20;i++){
        ans+=1ll*calc(i)*(1<<i);
    }
    printf("%lld\n",ans);
    return 0;
}