程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA 11400 Lighting System Design(貪心+DP)

UVA 11400 Lighting System Design(貪心+DP)

編輯:C++入門知識

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 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個電燈設計模式要設計,每個模式需要L盞燈,每盞燈花費C,然後每個模式需要在一個電壓下工作,每個電壓V,電壓花費K,一個電壓可以提供無限大的額定電壓,並且可以控制大小,換句話說,只要有一個電壓V,就可以用在小於V的所有模式中,問最小需要的花費是多少。

思路:貪心,首先知道大電壓可以用在小電壓上,先按電壓大小排個序,然後dp[i]表示前i個模式的最小花費,那麼我們知道當取到i的時候,i那個電壓是一定要買的,因為前面都比這個電壓小,不買就沒有滿足的了,所以問題轉化為那幾個電壓要買,那麼狀態轉移方程就出來了:

dp[i] = min{dp[j] + sum};這裡的sum為i到j之間模式的點燈花費(不用買電壓,因為用i的就可以了)。

代碼:

#include 
#include 
#include 
#define min(a,b) ((a)<(b)?(a):(b))
#define INF 0x3f3f3f3f
using namespace std;

const int N = 1005;
int n, dp[N];
struct D {
	int v, k, c, l;
} d[N];

bool cmp(D a, D b) {
	return a.v < b.v;
}

int solve(int now) {
	if (dp[now] != -1) return dp[now];
	dp[now] = INF;
	int sum = d[now].k;
	if (now == 0) return dp[now] = 0;
	for (int i = now; i >= 1; i--) {
		sum += d[now].c * d[i].l;
		int t = solve(i - 1);
		dp[now] = min(dp[now], t + sum);	
	}
	return dp[now];
}

int main() {
	while (~scanf("%d", &n) && n) {
		memset(dp, -1, sizeof(dp));
		for (int i = 1; i <= n; i++)
			scanf("%d%d%d%d", &d[i].v, &d[i].k, &d[i].c, &d[i].l); 
		sort(d + 1, d + n + 1, cmp);
		printf("%d\n", solve(n));
	}
	return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved