标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1536 Accepted Submission(s): 673
// // main.cpp // hdu5492 // // Created by Candy on 10/2/16. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <set> using namespace std; const int N=35,INF=1e9; inline int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();} return x; } int T,n,m,l,a[N][N],f[N][N][N*2*30]; int dp(){ memset(f,127,sizeof(f)); f[1][1][a[1][1]]=a[1][1]*a[1][1]; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) for(int k=0;k<=l*30;k++) if(f[i][j][k]<=INF){ int x=i+1,y=j,w=a[x][y]; f[x][y][k+w]=min(f[x][y][k+w],f[i][j][k]+w*w); x=i;y=j+1;w=a[x][y]; f[x][y][k+w]=min(f[x][y][k+w],f[i][j][k]+w*w); } int ans=INF; for(int k=0;k<=l*30;k++) if(f[n][m][k]<INF) ans=min(ans,l*f[n][m][k]-k*k); return ans; } int main(int argc, const char * argv[]) { T=read();int cas=0; while(T--){ n=read();m=read();l=n+m-1; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j]=read(); printf("Case #%d: %d\n",++cas,dp()); } return 0; }
标签:
原文地址:http://www.cnblogs.com/candy99/p/5927481.html