标签:
题目链接:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1557 Accepted Submission(s): 678
#include <bits/stdc++.h> using namespace std; const int inf=1e7; const int maxn=1e5+10; int n,m,a[40][40]; int dp[32][32][61*31]; int QAQ(int x){return x*x;} int main() { int t,Case=0; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]); int hi=(n+m)*31; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) for(int k=0;k<=hi;k++) dp[i][j][k]=inf; dp[1][1][a[1][1]]=QAQ(a[1][1]); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { for(int k=0;k<=hi;k++) { dp[i][j+1][k+a[i][j+1]]=min(dp[i][j+1][k+a[i][j+1]],dp[i][j][k]+QAQ(a[i][j+1])); dp[i+1][j][k+a[i+1][j]]=min(dp[i+1][j][k+a[i+1][j]],dp[i][j][k]+QAQ(a[i+1][j])); } } } int ans=inf; for(int i=0;i<=hi;i++)ans=min(ans,(n+m-1)*dp[n][m][i]-QAQ(i)); printf("Case #%d: %d\n",++Case,ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5954798.html