2011 Multi-University Training Contest 10 - Host by HRBEU hdu 3933 Dark Parth

这是一个动态规划的题,开始的时候状态找的还不错,但是边界控制的不是很好,wa了一会儿……

原题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3933】

源码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int dp[1010][110];
int sum[1010];
int main()
{
    int n;
    while(scanf("%d",&n) && n != 0)
    {
        int lb,la,s;
        scanf("%d%d%d",&lb,&la,&s);

        memset(sum,0,sizeof(sum));
        //memset(dp,0,sizeof(dp));
        for(int i = 0;i < n+2;i++)
            for(int j = 0;j <= s;j++)
                dp[i][j] = 0;
        for(int i = 0;i <= n+1;i++)
            dp[i][s+1] = 100000000;

        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&sum[i]);
            sum[i] += sum[i-1];
        }
        //dp[n][s] = sum[n] - sum[n-1];
        //dp[n][s-1] = 0;
        for(int i = n;i >= 1;i--)
        {
            for(int j = s;j >= 0;j--)
            {
                dp[i][j] = dp[i+1][j] + sum[i] - sum[i-1];
                //if(n - i <
                int a = i + la;
                int b = i + lb;
                //a--;b--;
                if(a > n+1)
                {
                    continue;
                }
                for(int k = a;k <= b && k <= n+1;k++)
                {
                    dp[i][j] = min(dp[i][j] ,dp[k][j+1]);
                }
            }
        }
        int re = 100000000;


        for(int i = 0;i <= s;i++)
        {
            re = min(re,dp[1][i]);
        }
        cout << re << "\n";
    }
    return 0;
}


 

你可能感兴趣的:(2011 Multi-University Training Contest 10 - Host by HRBEU hdu 3933 Dark Parth)