动态规划

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<time.h>
using namespace std;
#define inf 0x3f3f3f3f
int V[30][30],dp[30][30][1800];
int min(int a,int b){
    return a<b?a:b;
}
int main()
{
    freopen("input4.txt","r",stdin);   
    freopen("output.txt","w",stdout);
    int start=clock();//从这开始计时
    int ca=1,T,i,j,k,n,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<n;++i)
            for(j=0;j<m;++j) scanf("%d",&V[i][j]);
            printf("Case #%d: ",ca++);
            memset(dp,inf,sizeof(dp));
            dp[0][0][V[0][0]]=V[0][0]*V[0][0];
            for(i=0;i<n;++i)
                for(j=0;j<m;++j)
                {
                    if(i+1<n){
                        for(k=0;k<=59*30;++k)
                            if(dp[i][j][k]!=inf)
                                dp[i+1][j][k+V[i+1][j]]=min(dp[i+1][j][k+V[i+1][j]],dp[i][j][k]+V[i+1][j]*V[i+1][j]);
                    }
                    if(j+1<m){
                        for(k=0;k<=59*30;++k)
                            if(dp[i][j][k]!=inf)
                                dp[i][j+1][k+V[i][j+1]]=min(dp[i][j+1][k+V[i][j+1]],dp[i][j][k]+V[i][j+1]*V[i][j+1]);
                    }
                }
                int ans=inf;
                for(i=0;i<=59*30;++i)
                    if(dp[n-1][m-1][i]!=inf)
                        ans=min(ans,(n+m-1)*dp[n-1][m-1][i]-i*i);
                    printf("%d\n",ans);
    }
    int end=clock();//到这结束
    printf("%d ms\n",end-start);//算出来的单位是毫秒
    return 0;
}
View Code

 

#include<stdio.h>
#include<string.h>
#define inf 0x3f3f3f3f
int dp[50][50][1800];
int v[100][100];
int min(int a,int b){
    return a<b?a:b;
}
int main(){
    int t,n,m,cur=1,i,j,k;
    //freopen("input4.txt","r",stdin);   
    //freopen("output.txt","w",stdout);
    scanf("%d",&t);
    while(t--){
        printf("Case #%d: ",cur++);
        scanf("%d %d",&n,&m);
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++)
                scanf("%d",&v[i][j]);
        }
        memset(dp,inf,sizeof(dp));
        dp[1][1][v[1][1]]=v[1][1]*v[1][1];
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                if(j+1<=m){//向右
                    for(k=0;k<=59*30;k++){
                        if(dp[i][j][k]!=inf)
                            dp[i][j+1][k+v[i][j+1]]=min(dp[i][j+1][k+v[i][j+1]],dp[i][j][k]+v[i][j+1]*v[i][j+1]);
                    }
                }
                if(i+1<=n){//向下
                    for(k=0;k<=59*30;k++){
                        if(dp[i][j][k]!=inf)
                            dp[i+1][j][k+v[i+1][j]]=min(dp[i+1][j][k+v[i+1][j]],dp[i][j][k]+v[i+1][j]*v[i+1][j]);
                    }
                }
                
                
            }
        }
        int ans=inf;
        for(i=0;i<=59*30;i++){
            if(dp[n][m][i]!=inf)
                ans=min(ans,(n+m-1)*dp[n][m][i]-i*i);
        }
        printf("%d\n",ans);
        
    }
    return 0;
}
View Code