1. 程式人生 > >(動態規劃)UVA-11400:Lighting System Design

(動態規劃)UVA-11400:Lighting System Design

span design 依次 gin nbsp repl can lac ssi

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 to your design, you need lamps of n di?erent 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 in?nite number of lamps of its voltage rating.) and complete the design.

你接到了為會議禮堂設計照明系統的任務,一番精算之後你已設計完畢。在你的設計中,需要n種不同能量級別的燈。而依據某神奇的電流定律,所有燈需要通過等量電流,所以每種燈有一個對應的電壓等級。現在你知道每種燈的所需數量和單價,問題是你還得給每種燈買電壓源。每種燈有它對應的電壓源,一個源可以為無限盞燈供能。

But the accounts section of your company soon ?gures 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.

每組樣例先給出n,代表種類數目。接下來的n行每行將依次給出這種燈的參數——V:電壓級別、K:這種電壓的電壓源價格、C:這種燈泡的單價、L:所需這種燈泡的數量。

輸入的n==0時結束輸入。


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

樣例解釋:778 = 400 + (20+16+18)*7

只看表層題面不好解決,要進行一定的思考推演出萬變情況中不變的規律,方可寫出狀態轉移。

結論1:每種燈泡要麽不換,要麽全換。

如果單拿兩種燈泡,我們什麽情況換?在我們發現應該換的情況下,如何證明全換肯定比部分換省錢?不妨動筆寫寫。

結論2:現我們將輸入按照電壓等級從小到大排序後,j < i,如果 j 要換成 i,則從 j 到 i-1 都要換成 i。

假設 j 要換成 i,而 j+1 是獨立不換的,j 換成 i 比換成 j+1更好,那麽?

如果結論2成立,那麽意味著這個升序列中有幾個“斷層”,它們之間的弱勢燈泡要被替換為右側大功率燈泡。首先,第n個由於功率最大,肯定是最後一個斷層。然後,此時假設斷層 i 的上一個斷層為 j,由於 j 和 i 之間的燈泡都要換成 i,所以dp[i] = dp[j] + 燈泡數量*i的單價 + i的電壓源價格。

該暴力的時候也得暴力一下,上一個斷層 j 無法直接得出,可以遍歷1~i-1看看哪種方案最優得之。

故給出狀態轉移方程dp[i] = min{ dp[j] + (s[i]-s[j])*C[i] + K[i] }。其中s[i]是燈泡數量前綴和。

註:對於上述結論、思路有疑義或想法的同學歡迎討論!也歡迎分享其他解法,或者對題目難度進行反饋!

下面是我的代碼,歡迎優化、指正:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 struct category
 6 {
 7     int V, K, C, L;
 8 }a[1001];
 9 
10 int n;
11 int dp[1001], s[1001];
12 
13 int main()
14 {
15     while (~scanf("%d", &n) && n)
16     {
17         for (int i = 1; i <= n; i++)
18             scanf("%d%d%d%d", &a[i].V, &a[i].K, &a[i].C, &a[i].L);
19 
20         std::sort(a+1, a+1+n, [](const category& x, const category& y){return x.V < y.V;});
21 
22         memset(dp, 0, sizeof(dp));
23         for (int i = 1; i <= n; i++)
24             s[i] = s[i-1] + a[i].L;
25 
26         for (int i = 1; i <= n; i++)
27             for (int j = 0; j < i; j++)
28                 if (!dp[i] || dp[i] > dp[j] + (s[i]-s[j])*a[i].C + a[i].K)
29                     dp[i] = dp[j] + (s[i]-s[j])*a[i].C + a[i].K;
30 
31         printf("%d\n", dp[n]);
32     }
33 }

(動態規劃)UVA-11400:Lighting System Design