連結: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;
}