LightOJ 1295 Lighting System Design dp

链接: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 L(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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-29 19:06:31

LightOJ 1295 Lighting System Design dp的相关文章

LightOJ 1295 Lighting System Design (排序+dp)

题目链接:LightOJ 1295  Lighting System Desig 题意:给出n种灯(v,k,c,l)分别是灯的(电压,所需电源费用,灯的单价,所需灯的数量),电压高的灯可以代替电压低的灯但是电压低的灯不能代替电压高的等,每种灯的电压各种相同,问选n种灯最小的花费. 思路: 因为电压高的灯可以代替电压低的灯--按电压高到低排序, 然后求前缀和--因为当出现代替时可以,快速统计灯的花费. 然后就是dp--状态方程:dp[i] 买前i类灯的最少花费,sum[i]前缀和.dp[i]=mi

UVA 14000 Lighting System Design(DP)

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

UVa 11400 Lighting System Design(DP 照明系统设计)

题意  设计某个地方的照明系统  一共需要n种不同类型的灯泡  接着输入 每种灯泡的电压v  对应电压电源的价格k  每个灯泡的价格c   需要这种灯泡的数量l   电压低的灯泡可以用电压高的灯泡替换   每种灯泡只需要一个对应的电源   求完成这个照明系统的最少花费 比较简单的DP  容易知道 当要替换一种灯泡中的一个到令一种电压较高的灯泡时  只有全部替换这种灯泡为另一种时才可能使总花费变小   全部替换掉就省下了这种灯泡的电源花费   先把灯泡按照电压排序   那么每种灯泡都可以替换他前面

UVa 11400 Lighting System Design(DP 照明设计)

意甲冠军  地方照明系统设计  总共需要n不同类型的灯泡  然后进入 每个灯电压v  相应电压电源的价格k  每一个灯泡的价格c   须要这样的灯泡的数量l   电压低的灯泡能够用电压高的灯泡替换   每种灯泡仅仅须要一个相应的电源   求完毕这个照明系统的最少花费 比較简单的DP  easy知道 当要替换一种灯泡中的一个到令一种电压较高的灯泡时  仅仅有所有替换这样的灯泡为还有一种时才可能使总花费变小   所有替换掉就省下了这样的灯泡的电源花费   先把灯泡依照电压排序   那么每种灯泡都能够

UVA 11400 Lighting System Design DP

最优情况不可能跨过一种灯泡,为什么? 因为如果A换成C是划算的那么如果A换成B是不划算的那么可以将A和B都换成C,肯定是划算的= = 然后就是简单DP了. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <

【线性结构上的动态规划】UVa 11400 - Lighting System Design

Problem F Lighting System Design Input: Standard Input Output: Standard Output 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 e

uva 11400 - Lighting System Design(动态规划 最长上升子序列问题变型)

本题难处好像是在于 可以把一些灯泡换成电压更高的灯泡以节省电源的钱 ,所以也才有了对最优方案的探求 好的处理方法是按照电压从小到大排序,只能让前面的换成后面的,也就满足了把一些灯泡换成电压更高的灯泡 的要求: 一种电压的灯泡,要么不换,要换则应该全换:换,说明用当前的电源不值:而既然不值则应该全部换掉以避免使用当前电源,不然即增加了灯泡费用又没节省电源费用,亏大了... 状态转移详见代码 #include<cstdio> #include<cstring> #include<

(动态规划)UVA-11400:Lighting System Design

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 ?gured out the requirements for an energy-e?cient design that can properly illuminate the entire hall. According t

11400 - Lighting System Design(DP)

该题以电压v,使得枚举有序化,对于每一种灯泡,怎么判断是不是要用它呢? 如何形成递推呢? 我们知道,递推就是要用到之前早已存好的值来确定当前最优解,所以我们用d[i]表示用1~i种灯泡的最小费用. 由于每种灯泡要么使用,要么被别的灯泡替代,所以d[i] = min(d[i],d[j] + (s[i]-s[j])*a[i].c + a[i].k);   其中j < i  表示前j个先用最优方案买,然后第j+1~i个都用第i号灯泡. 由于第n个灯泡电压最高,无法被别的灯泡所替代,所以必定有这个灯泡,