hdu2571动态规划

125ms.... 太慢了。。。dp[i][j] = max(dp[i][j-1],dp[i-1][j],dp[i][k],1<k<j&&j%k==0);

#include <iostream>
#include <cstring>

using namespace std;

#define MAXN 21
#define MAXM 1005
#define INF -123456789

int n,m;
int C;

int map[MAXN][MAXM];
int dp[MAXN][MAXM];

void DP()
{
    memset(dp,INF,sizeof(dp));
    dp[1][0] = dp[0][1] = 0;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            int ans = max(dp[i-1][j],dp[i][j-1]);
            for(int k = 1; k < j; k++)
            {
                if(j % k == 0)
                {
                    ans = max(ans,dp[i][k]);
                }
            }
            dp[i][j] = ans + map[i][j];
        }
    }
}


int main()
{
    cin>>C;
    while(C--)
    {
        memset(map,0,sizeof(map));
        cin>>n>>m;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                cin>>map[i][j];
            }
        }
        DP();
        cout<<dp[n][m]<<endl;
    }
    return 0;
}

 





posted @ 2013-08-12 19:22  pangbangb  阅读(167)  评论(0编辑  收藏  举报