Title Link: Lightoj 1295 Lighting System Desig
Test instructions: Give the n kinds of lamps (V,K,C,L) is the lamp (voltage, the required power cost, the unit price of the lamp, the number of lights required), the high voltage lamp can replace the low voltage lamp but the low voltage lamp cannot replace the voltage is high, each kind of lamp voltage are all the same, ask to choose the minimum cost of n lamps.
Ideas:
Because a high-voltage lamp can replace a low-voltage lamp-Sort by voltage high to low,
Then ask for the prefix and-----because when the substitution occurs, the cost of the lamp can be quickly counted.
Then there is the dp--state equation: dp[i] The minimum cost of the pre-purchase Class I lamps, sum[i] prefix and. Dp[i]=min (dp[i],dp[j-1]+ (sum[i]-sum[j-1]) *P[J].C+P[J].K);
AC Code:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;struct node { int V , K,c,l;}; struct node P[1010];bool CMP (node A,node b) { return A.V>B.V;} int dp[1010];//The minimum price of the first class I lamp int sum[1010];int main () { int j,cas=1; int t,i,n; scanf ("%d", &t); while (t--) { scanf ("%d", &n); for (i=1;i<=n;i++) scanf ("%d%d%d", &P[I].V,&P[I].K,&P[I].C,&P[I].L); Sort (p+1,p+n+1,cmp); for (i=1;i<=n;i++) { sum[i]=sum[i-1]+p[i].l; } dp[1]=p[1].k+p[1].c*p[1].l;dp[0]=0; for (i=2;i<=n;i++) {for (j=i;j>=1;j--) { int tmp=sum[i]-sum[j-1];//[j,i] if (j==i) dp[i]=dp[j-1 ]+TMP*P[J].C+P[J].K; else { dp[i]=min (DP[I],DP[J-1]+TMP*P[J].C+P[J].K); }}} printf ("Case%d:%d\n", Cas++,dp[n]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Lightoj 1295 Lighting System Design (sort +DP)