uva 11400 Lighting System Design

來源:互聯網
上載者:User

原題:
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 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, 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 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.
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
題目大意:
讓你給一個大廳安裝照明系統,給你n種不同的燈,其中每種燈有如下資料,電壓值,電源的價格,燈的單價,需要這個燈的數量。一個電源可以供應無數個燈,所有通過燈的電流都相同。為了節省開銷,你可以把一些燈換成電壓值更高的燈泡來代替(此時價格就按電壓高的燈泡的價格,數量依然是被替換的那個燈泡的數量)。現在問你要安裝這些數目的燈,最少要多少錢。

#include <bits/stdc++.h>using namespace std;const int inf=9999999;struct light{    int v,k,c,l;};light ls[1001];int cmp(const light &l1,const light &l2){    return l1.v<l2.v;}int dp[1001];int main(){    ios::sync_with_stdio(false);    int n;    while(cin>>n,n)    {        for(int i=1;i<=n;i++)            cin>>ls[i].v>>ls[i].k>>ls[i].c>>ls[i].l;        sort(ls+1,ls+1+n,cmp);        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;i++)        {            dp[i]=inf;            int tmp=0;            for(int j=i;j>=1;j--)            {                tmp+=ls[j].l;                dp[i]=min(dp[i],dp[j-1]+tmp*ls[i].c+ls[i].k);            }        }        cout<<dp[n]<<endl;    }    return 0;}

解析:
簡單的一維動態規劃問題,可以參考最長遞增子序列類似的那個疊箱子的問題。首先根據電壓值從小到大排序,因為電壓大的電源能夠接電壓要求下的燈泡。設定狀態dp[i]表示用到第i個燈泡時花費的最少錢數。接下來找狀態轉移,轉移過程只有兩種,要麼用當前燈泡的去替換後面的燈泡(因為排序過),要麼不替換,如果替換,替換到第多少個。這裡有個問題,比如一個燈泡需要20個,那麼如果要替換這個燈泡,是全部替換,還是替換一部分才能找到最優呢。答案肯定是全部替換,因為如果當前燈泡價格1,要被替換的價格是2,那肯定是1塊錢的便宜,而且還能省下一個被替換的燈泡的電源錢。
那麼

dp[i]=min(dp[i],dp[j-1]+tmp*light[i].c+light[i].k)

其中light[i]是第i種燈,tmp表示計算從i往後累加的燈泡數量,加一起用來被第i個燈泡替換。所以方程的意思就是dp[i]的最優值等於前j個燈泡用第i個燈泡替換加上j-1到第一個燈泡布置照明系統時的最優值(1<=j<=i)
這裡有一點要想清楚,比如現在又100個排好序的燈泡,現在要算dp[80],往前面尋找狀態,比如尋找到了dp[50],那麼酸的dp[80]就等於80到第51個燈泡的都用第80號燈泡代替再加上dp[50],那麼在80到50之間是否存在有的燈泡不用第80號燈泡代替來獲得最優值呢。當然存在,因為在你算到第50號之前已經枚舉到了這個狀態(比如第70個燈泡,此時為tmp為80到71個燈泡用第80號燈泡代替加上dp[70]的最優值),所以繼續往下枚舉即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.