码迷,mamicode.com
首页 > 其他好文 > 详细

POJ #1260 - Pearls

时间:2014-07-03 09:32:27      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   art   

DP is, enumeration + pruning. If you can think of enumeration pattern of a problem, plus a recurrence relation later, you are there.

Main ref: http://blog.csdn.net/kindlucy/article/details/5761795

The basic scenario is non-optimized case; all other possible cases, are optimized, and we try each solution of that.

bubuko.com,布布扣
//    1260 - Pearls
//    http://blog.csdn.net/kindlucy/article/details/5761795
//
#include <stdio.h>

#define MAX_CAT 100
#define Min(a,b) (a) < (b) ? (a) : (b)

int calc(int cnt, int ai[MAX_CAT], int pi[MAX_CAT])
{    
    int sum[MAX_CAT] = { 0 };
    for (int i = 1; i <= cnt; i++)
    {
        sum[i] = sum[i - 1] + ai[i - 1];
    }

    int dp[MAX_CAT + 1] = { 0 };
    for (int i = 1; i <= cnt; i++)    // dp[i]
    {
        int currmin = (ai[i - 1] + 10) * pi[i - 1] + dp[i - 1];    // non-optimized
        for (int j = 0; j < i; j ++)    // try each replacement solution
        {
            int currtry = dp[j] + (sum[i] - sum[j] + 10) * pi[i - 1];
            currmin = Min(currmin, currtry);
        }
        dp[i] = currmin;
    }

    return dp[cnt];
}

int main()
{
    int n; scanf("%d", &n);
    while (n--)
    {
        int ai[MAX_CAT] = { 0 };
        int pi[MAX_CAT] = { 0 };

        int cnt; scanf("%d", &cnt);
        for (int i = 0; i < cnt; i++)
        {
            scanf("%d%d", ai + i, pi + i);
        }

        int ret = calc(cnt, ai, pi);
        printf("%d\n", ret);
    }
    return 0;
}
View Code

POJ #1260 - Pearls,布布扣,bubuko.com

POJ #1260 - Pearls

标签:style   blog   http   color   os   art   

原文地址:http://www.cnblogs.com/tonix/p/3818256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!