The problem with voltage V, so that the enumeration ordered, for each kind of bulb, how to determine whether to use it? How to form recursion?
We know that recursion is to use the values that have been stored well before to determine the current optimal solution, so we use d[i] to indicate the minimum cost of 1~i bulbs.
Since each bulb is either used or replaced by another bulb, d[i] = min (D[i],d[j] + (S[i]-s[j]) *a[i].c + A[I].K); Where J < I means that the first J first buy with the best plan, and then the first j+1~i to use the No. I bulb.
Because the nth lamp voltage is the highest, can not be replaced by other bulbs, so there must be this light bulb, obviously this recursive relationship is rigorous. It makes perfect use of all the previously stored optimal solutions.
See the code for details:
#include <bits/stdc++.h>using namespace Std;const int maxn = + 10;const int INF = 2000000000;int N,d[maxn],s[max n];struct node{ int v,k,c,l;} A[maxn];bool CMP (node A,node b) { return a.v < B.V;} int main () { while (~scanf ("%d", &n) &&n) {for (int i=1;i<=n;i++) scanf ("%d%d%d%d", &A[I].V, &A[I].K,&A[I].C,&A[I].L); Sort (a+1,a+1+n,cmp); S[0] = 0; D[0] = 0; for (int i=1;i<=n;i++) s[i] = s[i-1] + a[i].l; for (int i=1;i<=n;i++) { d[i] = INF; for (int j=0;j<i;j++) { D[i] = min (D[i],d[j] + (S[i]-s[j]) *a[i].c + A[I].K); } } printf ("%d\n", D[n]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
11400-lighting System Design (DP)