uva 11400 - Lighting System Design

The meaning of problems: gives the n patterns, each pattern voltage v, the voltage charges k, c of each lamp and a lamp takes several l. Then a high voltage may be used for low voltage. To say the least ask how much it costs to meet the n mode.

Analysis: Each bulb voltage change of either all or none of change, or both sources of power are not. Since the low voltage lamp with a higher power may be. Press discharge voltage from low to high again. Set s [i] i before the total number of lamp types, d [i] is the lamp 1 to the minimum cost i, d [i] = min (d [j] + (s [i] -s [j]) * c [i] + k [i]), before the j-th first with the optimal solution, the latter j + 1 ~ i No. I are used in the power supply.

Code:

#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 1000 + 5;

struct Lamp
{
    int v, k, c, l;
    bool operator < (const Lamp& rhs) const
    {
        return v < rhs.v;
    }
} lamp[maxn];

int n, s[maxn], d[maxn];

int main()
{
    while(cin >> n && n)
    {
        for(int i = 1; i <= n; i++)
            cin >> lamp[i].v >> lamp[i].k >> lamp[i].c >> lamp[i].l;
        sort(lamp+1, lamp+n+1);
        s[0] = 0;
        for(int i = 1; i <= n; i++) s[i] = s[i-1] + lamp[i].l;
        d[0] = 0;
        for(int i = 1; i <= n; i++)
        {
            d[i] = s[i] * lamp[i].c + lamp[i].k; // 前i个灯泡全买类型i
            for(int j = 1; j <= i; j++)
                d[i] = min(d[i], d[j] + (s[i] - s[j]) * lamp[i].c + lamp[i].k);
        }
        cout << d[n] << "\n";
    }
    return 0;
}


 

Guess you like

Origin blog.csdn.net/tianwei0822/article/details/94433431