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
3ios
100 500 10 20ide
120 600 8 16ui
220 400 7 18this
0
Sample Output
778spa
題意:排序
就是說先給你一個n,表明這個系統中有n中類型的燈泡,v是這種類型燈泡的電壓,每種電壓的燈泡有須要一個變壓器吧(我本身理解的),無論這種類型的燈泡有多少個,只須要一個這種電壓變壓器就能夠了,第三個值是安裝一個這樣的燈泡多少錢,第四個值是目前這個系統中有這種燈泡多少個ci
咱們能夠用電壓大的燈泡去代替電壓小的燈泡,求最後這套系統的最小花費rem
題解:input
那確定若是某種燈泡要換那麼確定是一會兒把這種燈泡一下換完(想一想就知道了),那麼咱們對數據先初始化,對其電壓從小到大排序,以後再求前i中燈泡的前綴和string
每種燈泡要麼換要麼不換
設sum[i]爲前i種燈泡的總數量(即L值之和),d[i]爲燈泡1~i的最小花費,
動態轉移方程就是d[i]=min(d[j]+(sum[i]-sum[j])*c[i]+k[i],d[i]);
代碼:
1 //要先排序,後求和<_> 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<vector> 7 #include<queue> 8 using namespace std; 9 typedef long long ll; 10 const int maxn=1005; 11 const int INF=0x3f3f3f3f; 12 int dp[maxn],v[maxn],ans[maxn]; 13 struct shudui 14 { 15 int v,k,c,l; 16 } m[maxn]; 17 bool mmp(shudui x,shudui y) 18 { 19 return x.v<y.v; 20 } 21 int main() 22 { 23 int n; 24 while(~scanf("%d",&n)) 25 { 26 if(n==0) break; 27 int sum=0; 28 for(int i=1; i<=n; ++i) 29 scanf("%d%d%d%d",&m[i].v,&m[i].k,&m[i].c,&m[i].l); 30 //ans[i]=m[i].k+m[i].c*m[i].l; 31 sort(m+1,m+1+n,mmp); 32 for(int i=1;i<=n;++i) 33 v[i]=v[i-1]+m[i].l; 34 memset(dp,INF,sizeof(dp)); 35 dp[0]=0; 36 for(int i=1;i<=n;++i) 37 { 38 for(int j=0;j<i;++j) 39 { 40 dp[i]=min(dp[i],dp[j]+(v[i]-v[j])*m[i].c+m[i].k); 41 } 42 } 43 printf("%d\n",dp[n]); 44 } 45 return 0; 46 }