天天看点

Lighting System Design(dp)

You are given the task to design a lighting system for a huge conference hall. After doing a lot of

calculation and sketching, you have figured out the requirements for an energy-efficient design that

can properly illuminate the entire hall. According to your design, you need lamps of n different power

ratings. For some strange current regulation method, all the lamps need to be fed with the same

amount of current. So, each category of lamp has a corresponding voltage rating. Now, you know the

number of lamps and cost of every single unit of lamp for each category. But the problem is, you are

to buy equivalent voltage sources for all the lamp categories. You can buy a single voltage source for

each category (Each source is capable of supplying to infinite number of lamps of its voltage rating.)

and complete the design. But the accounts section of your company soon figures out that they might

be able to reduce the total system cost by eliminating some of the voltage sources and replacing the

lamps of that category with higher rating lamps. Certainly you can never replace a lamp by a lower

rating lamp as some portion of the hall might not be illuminated then. You are more concerned about

money-saving than energy-saving. Find the minimum possible cost to design the system.

Input

Each case in the input begins with n (1 ≤ n ≤ 1000), denoting the number of categories. Each of the

following n lines describes a category. A category is described by 4 integers - V (1 ≤ V ≤ 132000), the

voltage rating, K (1 ≤ K ≤ 1000), the cost of a voltage source of this rating, C (1 ≤ C ≤ 10), the cost

of a lamp of this rating and L (1 ≤ L ≤ 100), the number of lamps required in this category. The input

terminates with a test case where n = 0. This case should not be processed.

Output

For each test case, print the minimum possible cost to design the system.

Sample Input

3

100 500 10 20

120 600 8 16

220 400 7 18

Sample Output

778

题目大概:

需要安装n个灯来照明。每个灯给出属性v k c l,分表代表,该灯的电压,该电压总电源费用,该灯单价,需要安装的灯的数量。一个电压的电源可以供给无数的同电压的灯。现在可以用电压高的灯替换电压低的灯,问最少的安装灯的费用是多少。

思路:

可以按照电压排序后dp。

dp【i】代表前i个灯的最小费用。

那么,dp【i】的最小费用怎么算那,他来自与min(dp【j】+k【i】+(l【j+1】....+l【i】)*c【i】)。即把从j开始到i的所有灯,都替换后,换成一个电源k【i】,都用c【i】单价的灯,当然,要遍历所有小于i的灯,求出最小值。那么这种状态转移为什么对那,可以用反正法,简单证明一下在 1 2 3灯中,1  3用一种灯替换,2 用一种灯替换,是否成立即可。

代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=1100;
int dp[maxn];
struct poin
{
    int v,k,c,l;
}G[maxn];
int cmp(poin a,poin b)
{
    return a.v<b.v;
}
int sum[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
    memset(dp,0,sizeof(dp));
    memset(sum,0,sizeof(sum));
    memset(G,0,sizeof(G));
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d",&G[i].v,&G[i].k,&G[i].c,&G[i].l);
    }
    sort(G+1,G+1+n,cmp);
    for(int i=1;i<=n;i++)
    {
        sum[i]=sum[i-1]+G[i].l;
    }
    for(int i=1;i<=n;i++)
    {
        dp[i]=G[i].k+sum[i]*G[i].c;
        for(int j=1;j<i;j++)
        {
            dp[i]=min(dp[i],dp[j]+G[i].k+(sum[i]-sum[j])*G[i].c);
        }
    }
    printf("%d\n",dp[n]);
    }
    return 0;
}