链接:http://lightoj.com/volume_showproblem.php?problem=1295
1295 - Lighting System Design
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 and cost of every single unit of lamp for each category. But the problem is that 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 rather than energy-saving. Find the minimum possible
cost to design the system.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 1000). Each of the next n lines contains four integers Vi, Ki, Ci and Li (1 ≤ Vi≤
105, 1 ≤ Ki ≤ 1000, 1 ≤ Ci ≤ 10, 1 ≤ Li ≤ 100). Here the integers in ith line have the following meaning.
1. Vi means the voltage rating,
2. Ki means the cost of a voltage source of this category,
3. Ci means the cost of a lamp of this category and
4. Li means the number of required lamps of this category.
You can assume that the voltage rating for the categories will be distinct.
Output
For each case, print the case number and the minimum possible cost to design the system.
Sample Input |
Output for Sample Input |
1 3 100 500 10 20 120 600 8 16 220 400 7 18 |
Case 1: 778 |
题意:
有若干个灯,每个灯有四个值
V 该灯泡的电压,可以买电压高的灯泡代替电压低的灯泡。 电压两两不同
K 发电机价格,只有有一台,就可以供应无限多个该电压的灯泡。
C 灯泡价格
L 这个电压的灯泡需要多少只
问买完所有要求的灯泡的最小花费
做法
因为高电压可以代替低电压的灯泡,所以高电压可以后判断要不要买发电机。
所以先按电压排个序,那么就是后面一定可以代替前面的了。
预处理下灯泡数的前缀和 sum数组。
然后for两层,
dp[i]=min(dp[i],dp[j]+la[i].k+la[i].c*(sum[i]-sum[j]));
表示买了i发电机,dp[j]表示买好前j个灯泡的最优解,然后加上买发电机的钱,再加上购买剩下的灯泡的钱,就是总的花费了。取个最小值就是购买前i个灯泡的最优解了。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream> #include <algorithm> using namespace std; #include <stack> #include <queue> #include <vector> #include <deque> #include <set> #include <map> struct lamp { int v,k,c,l; }; lamp la[1010]; int sum[1010]; int dp[1010]; int cmp(lamp a,lamp b) { return a.v<b.v; } int main() { int t; int cas=1; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d%d%d",&la[i].v,&la[i].k,&la[i].c,&la[i].l); } sort(la+1,la+n+1,cmp); sum[0]=0; for(int i=1;i<=n;i++) sum[i]=sum[i-1]+la[i].l; memset(dp,0x7f7f7f7f,sizeof dp); //printf("%d\n",dp[1]); dp[0]=0; for(int i=1;i<=n;i++) { for(int j=0;j<i;j++) { dp[i]=min(dp[i],dp[j]+la[i].k+la[i].c*(sum[i]-sum[j])); } } printf("Case %d: %d\n",cas++,dp[n]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。