You are given the task to design a lighting system for a huge conference hall. After doing a lot of calculation & 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 & 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.) & 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 & 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 & 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 Output for Sample Input
3 100 500 10 20 120 600 8 16 220 400 7 18 0 | 778 |
问题:设计一个照明系统。一共有n种灯泡可以选择,不同种类的灯泡必须用不同的电源,但同一种灯泡可共用同一电源。每种灯泡有四个数值表示:电压v,电源费用k,每个灯泡的费用c,和所需灯泡的数量l。
通过所有灯泡的电流都相同,为了省钱可以一些灯泡换成电压更高的另一种灯泡用来节省电源的钱(但是不能换成电压低的灯泡)。计算出最优方案的费用。
分析:首先得到一个结论:每种电压的灯泡,要么全换,要么全不换。因为如果只换一部分,那么两种电源都需要。既然可以省钱,为什么不全部换呢。首先按照电压的大小排序,sum[i]为前i种灯泡总数量,dp[i]为前i种灯泡的最优开销。
d[i]=min{d[i],d[j]+(s[i]-s[j])*c[i]+k[i]}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
int dp[1005];
int sum[1005];
struct stu{
int v,k,c,l;
}bulb[1005];
bool cmp(struct stu a,struct stu b){
return a.v<b.v;
}
int main(){
int n;
int i,j,k;
while(~scanf("%d",&n)&&n){
for(i=1;i<=n;i++){
scanf("%d %d %d %d",&bulb[i].v,&bulb[i].k,&bulb[i].c,&bulb[i].l);
}
sort(bulb+1,bulb+n+1,cmp);
sum[1]=bulb[1].l;
dp[0]=0;
dp[1]=99999999;
for(i=2;i<=n;i++){
sum[i]=bulb[i].l;
sum[i]+=sum[i-1];
dp[i]=99999999;
}
for(i=1;i<=n;i++){
for(j=0;j<i;j++){
dp[i]=min(dp[i],dp[j]+(sum[i]-sum[j])*bulb[i].c+bulb[i].k);
}
}
printf("%d\n",dp[n]);
memset(dp,0,sizeof(dp));
memset(sum,0,sizeof(sum));
}
return 0;
}