Question:
There are n kinds of bulbs. Different types of bulbs must use different power supplies, but the same type of bulbs can use the same power supply. Each bulb has four parameters:
Voltage value V, power supply cost K, cost of each light bulb C, number of required bulbs L
To save money, you can use bulbs with high voltage to replace bulbs with low voltage. Minimum output cost.
Analysis:
The bulb for each type of power supply is either not changed or all changed, because if only the part is changed, both types of power supply should be bought, which is not cost-effective.
Sort the voltage from small to large. s [I] indicates the total number of bulbs required for the first I bulbs. d [I] indicates the minimum cost for the first I bulbs.
D [I] = min {d [J] + (s [I]-s [J]) * C [I] + k [I]} (j = 0, I-1) (representing the optimal solution for the first J bulbs, J + 1 ~ The I type is replaced by the I type bulb and the I type is used)
1 // # define local 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <algorithm> 6 using namespace STD; 7 8 const int maxn = 1000 + 10; 9 struct lamp10 {11 int v, k, C, L; 12 bool operator <(const lamp &) const13 {14 return v <. v; 15} 16} lamps [maxn]; 17 int s [maxn], d [maxn]; 18 19 int main (void) 20 {21 # ifdef local22 freopen ("11400in.txt", "r", stdin); 23 # endif24 25 int N; 26 W Hile (scanf ("% d", & n) = 1 & N) 27 {28 for (INT I = 1; I <= N; ++ I) 29 scanf ("% d", & lamps [I]. v, & lamps [I]. k, & lamps [I]. c, & lamps [I]. l); 30 sort (lamps + 1, lamps + 1 + n); 31 s [0] = 0; 32 for (INT I = 1; I <= N; + + I) s [I] = s [I-1] + lamps [I]. l; 33 34 D [0] = 0; 35 for (INT I = 1; I <= N; ++ I) 36 {37 d [I] = d [I-1] + lamps [I]. C * lamps [I]. L + lamps [I]. k; // use the I-th bulb and power supply 38 for (Int J = 0; j <I; ++ J) 39 {// use the first J bulb Optimal Solution, J + 1 ~ The I type is replaced by the I type bulb and 40 d [I] = min (d [I], d [J] + (s [I]-s [J]) * lamps [I]. c + lamps [I]. k); 41} 42} 43 printf ("% d \ n", d [N]); 44} 45 46 Return 0; 47}
Code Jun
UV 11400 Lighting System Design