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
3
100 500 10 20
120 600 8 16
220 400 7 18
0
Sample Output
778
题意:你的任务是设计一个照明系统。一共有n(n≤1000)种灯泡可供选择,不同种类的灯泡必须用不同的电源,但同一种灯泡可以共用一个电源。每种灯泡用4个数值表示:电压值V(V≤132000),电源费用K(K≤1000),每个灯泡的费用C(C≤10)和所需灯泡的数量L(1≤L≤100)。
假定通过所有灯泡的电流都相同,因此电压高的灯泡功率也大。为了省钱,可以把一些灯泡换成电压更高的另一种灯泡以节省电源的钱(不能换成电压更低的灯泡)。计算出最优方案的费用。
题解:
这题要明确很重要的一点:一种灯泡要么全部换,要么不换。若替代灯泡能省钱,全部替换完能省更多,替换一部分反而会多算电源费。
按电压大小排序,小的在前。再处理出前i种灯泡的总需求量。
dp[i]: 前i种灯泡的费用
dp[i]=min( dp[j]+light[i].C*( light[i].sum-light[j].sum )+light[i].K )
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> using namespace std; const int INF=0x3f3f3f3f; const int N=1010; int n, m, sum, dp[N]; struct node{ int V, K, C, sum; }light[N]; bool cmp( node a, node b ) { return a.V<b.V; } int main() { while( ~scanf( "%d", &n ) ) { if( !n ) break; for( int i=1; i<=n; i++ ) scanf( "%d%d%d%d", &light[i].V, &light[i].K, &light[i].C, &light[i].sum ); [cpp] view plain copy sort( light+1, light+n+1, cmp ); [cpp] view plain copy for( int i=1; i<=n; i++ ) light[i].sum+=light[i-1].sum; [cpp] view plain copy [cpp] view plain copy memset( dp, INF, sizeof dp ); dp[0]=0; for( int i=1; i<=n; i++ ) for( int j=0; j<i; j++ ) dp[i]=min( dp[i], dp[j]+light[i].C*(light[i].sum-light[j].sum)+light[i].K ); printf( "%d\n", dp[n] ); } return 0;
[UVa11400]照明系统设计
最新推荐文章于 2024-09-27 18:45:48 发布