UVA 11400 (DP)

Source: Internet
Author: User

Test instructions given n types of light bulbs, each bulb gives its voltage V, the power cost K, the cost of each lamp C and the demand L, now by using a large voltage bulb to replace some of the small voltage bulb to reduce the total cost, to find the minimum cost.

First of all, it is easy to prove that, for the minimum cost, it is possible to replace or replace all of the bulbs.

This problem is difficult to find in how to look for sub-problems. If the lamp voltage first to order from small to large, the definition of dp[i] to replace the first I bulbs after the minimum cost, because of a replacement, do not know whether to replace the bulb will not be used later, if not the equivalent of non-replacement in the case of more than a power supply, which does not necessarily achieve optimal, So the definition does not work. so the current decision will affect the subsequent decisions.  In this case, we should make certain restrictions on the decision: the minimum cost of the first I light bulb, only allow the replacement of the first light bulb! How to replace it? can only replace 1 to I in serial number of bulbs!!! That is, the decision should be to choose a J, replace all the light bulbs with the number J to i-1, so that the first bulb cost is minimal. Then the final dp[n] is the answer. i.e. D[i] to consider only the minimum cost of the first I bulbs, the state transfer equation: d[i] = Min{d[i] + (S[i]-s[j]) * C[i] + k[i]} where S[i] is the total number of previous I bulbs.

But is it correct to do so, first of all, the above method in seeking the minimum cost of the first I bulb, can only be replaced with the first light bulb, this does not affect, because through the cycle of all the bulbs will be used to replace. The problem is that only the replacement of a continuous interval bulb causes the loss of the solution. For example, will not appear the optimal solution is the sequence number is 1, 3 of the bulb is replaced by a lamp, the number 2 is replaced by another or not replaced. Assuming that this is the minimum cost, then the replacement of the bulb 2 of the unit price must be less than the replacement of the bulb 1 of the unit price, otherwise it will not be the minimum value, since the replacement sequence 2 of the lamp unit price is less than the replacement number 1 lamp unit price, then replace the 2 bulb to replace the lamp 1, will produce a smaller This is inconsistent with assumptions, so the hypothesis is not true, that is, the previous decision will not miss the answer. The code is as follows:

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace std;const int maxn = 1010;struct lamb{    int v;    int C;    int l;    int k;}; Lamb La[maxn];int S[MAXN], N, Dp[maxn];bool camp (Lamb A, Lamb b) {    return a.v < B.V;} int main () {    while (true)    {        scanf ("%d", &n);        if (!n) break            ;        int I, J;        for (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, camp);        for (i = 1; I <= n; i++)        {            s[i] = s[i-1] + la[i].l;        }        for (i = 1; I <= n; i++)        {            Dp[i] = la[i].k + la[i].c * s[i];   Initialized to the value of the first light bulb. For            (j = 1; j < I; j + +)            {                Dp[i] = min (Dp[i], Dp[j] + (S[i]-s[j]) * la[i].c + LA[I].K);            }        }        printf ("%d\n", Dp[n]);    }    return 0;}


UVA 11400 (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.