POJ #1260 型如" E[j] = opt{D+w(i,j)} "的简单DP 线性DP

Description


 

In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jewelry with pearls in it. The Royal Pearl has its name because it delivers to the royal family of Pearlania. But it also produces bracelets and necklaces for ordinary people. Of course the quality of the pearls for these people is much lower then the quality of pearls for the royal family.In Pearlania pearls are separated into 100 different quality classes. A quality class is identified by the price for one single pearl in that quality class. This price is unique for that quality class and the price is always higher then the price for a pearl in a lower quality class. 
Every month the stock manager of The Royal Pearl prepares a list with the number of pearls needed in each quality class. The pearls are bought on the local pearl market. Each quality class has its own price per pearl, but for every complete deal in a certain quality class one has to pay an extra amount of money equal to ten pearls in that class. This is to prevent tourists from buying just one pearl. 
Also The Royal Pearl is suffering from the slow-down of the global economy. Therefore the company needs to be more efficient. The CFO (chief financial officer) has discovered that he can sometimes save money by buying pearls in a higher quality class than is actually needed.No customer will blame The Royal Pearl for putting better pearls in the bracelets, as long as the 
prices remain the same. 
For example 5 pearls are needed in the 10 Euro category and 100 pearls are needed in the 20 Euro category. That will normally cost: (5+10)*10+(100+10)*20 = 2350 Euro.Buying all 105 pearls in the 20 Euro category only costs: (5+100+10)*20 = 2300 Euro. 
The problem is that it requires a lot of computing work before the CFO knows how many pearls can best be bought in a higher quality class. You are asked to help The Royal Pearl with a computer program. 

Given a list with the number of pearls and the price per pearl in different quality classes, give the lowest possible price needed to buy everything on the list. Pearls can be bought in the requested,or in a higher quality class, but not in a lower one. 

Input

The first line of the input contains the number of test cases. Each test case starts with a line containing the number of categories c (1<=c<=100). Then, c lines follow, each with two numbers ai and pi. The first of these numbers is the number of pearls ai needed in a class (1 <= ai <= 1000). 
The second number is the price per pearl pi in that class (1 <= pi <= 1000). The qualities of the classes (and so the prices) are given in ascending order. All numbers in the input are integers. 

Output

For each test case a single line containing a single number: the lowest possible price needed to buy everything on the list. 

Sample Input

2
2
100 1
100 2
3
1 10
1 11
100 12

Sample Output

330
1344

翻译如下:

  有n个等级的珠宝,等级依次升高,等级越高价钱越高,每买一个等级的任何数量的珠宝必须多付10颗此种珠宝的价钱,可以用高等级的珠宝代替低等级的,问要买到若干规定的数量和等级珠宝的最少花费。例如买5颗价值为10的、100颗价值为20的珠宝,有两种方案:一种为分别买两种等级的珠宝价钱为(5+10)*10+(100+10)*20 = 2350;另一种是将等级低的(即价格低)的珠宝全部换为等级高的,此时价钱为(5+100+10)*20 = 2300,故第二种方案较优。

 

思路


 

  首先注意这道题的输入中珠宝的等级以及价格是递增的,所以不存在把第 i 种并入第 i+2 种、i+3 种的情况,因为第 i 种并入第 i+1 种是最优的。

  这道题很容易被误用贪心解题,以为每一次的决策就是在 “前i-1 种并入第 i 种时的费用”与“前i-1种不并入第 i 种时的费用” 之中选费用更小的那个。实际上“并入”的操作是存在“断点”的,举个例子:

第一种:  1    1
第二种:  1   20
第三种:100   21

  如果用贪心解,那么在第三个阶段时,贪心在:

    1、前2种并入第3种时的费用;

    2、前2种不并入第3种时的费用;

  这两种情况中取最小值。但是最优的买法是第 1 种不并入第 2 种、第 2 种并入第 3 种。用贪心的话是不会考虑“断点”的存在的,它会将前 2 种看成一个整体并入第 3 种,而不会把第 1 种和第 2 种拆开再将第 2 种并入第 3 种。

  显然贪心是没法处理“断点”问题的,那么就想到了DP,在买第 3 种之前,先把前 2 种的最优买法记录下来,之后的决策就是在“前 i-1 种并入第 i 种”、“前i-1种不并入第 i 种”之中进行选择。那么状态定义为前 i 种珠宝的最小花费,枚举计算并入第 i 种的费用。拿上面的例子说明这个枚举过程:把第 1 ~2 种并入第 3 种,只第 2 种并入第 3 种,前两种都不并入第 3 种,之后取其中的最小值。这种做法的好处是把断点的情况考虑进去了。同时时间比暴力枚举算法又少了很多,因为把前 i 种的最优解都已经记录下来,不用重复求解。

  

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define INF 1000000000

struct Pearl {
    int ai; //数目
    int pi; //价格
};
const int MAX_C = 105;
long long dp[MAX_C]; //前i种珍珠的最小花费

int main(void){
    int test;
    cin >> test;
    while (test--) {
        int c; //珍珠共c种
        cin >> c;
        vector<Pearl> l; // Pearl list
        l.resize(c+1);
        for (int i = 1; i <= c; i++) {
            cin >> l[i].ai >> l[i].pi;
        }
        long long sum[MAX_C+1]; //前i种珍珠的个数之和
        sum[0] = 0;
        sum[1] = l[1].ai;
        for (int i = 2; i <= c; i++) {
            sum[i] = sum[i-1] + l[i].ai;
        }
        dp[0] = 0;
        dp[1] = (l[1].ai + 10) * l[1].pi;
        for (int i = 2; i <= c ; i++) {
            dp[i] = INF;    
        }
        //计算前 i 种珍珠的最小费用
        for (int i = 2; i <= c; i++) {
            //枚举第 j+1 ~ i 种珍珠并入第i种珍珠时的费用
            //j+1 = i 时相当于除第i种外其他种类的珍珠都不并入第i种
            for (int j = 0; j < i; j++) {
                dp[i] = std::min(dp[i], (sum[i] - sum[j] + 10)*l[i].pi + dp[j] );
            }
        }

        cout << dp[c] << endl;
        l.clear();
        vector<Pearl>().swap(l);
    }
    return 0;
}
View Code

 

posted @ 2018-02-08 10:09  bw98  阅读(229)  评论(0编辑  收藏  举报