Lighting System Design UVA - 11400 动态规划

题目:题目链接

思路:简单的动态规划问题,先把灯泡按照电压从小到大排序。设s[i]为前i种灯泡的总数量(即L值之和),d[i]为灯 泡1~i的最小开销,则d[i] = min{d[j] + (s[i]-s[j])*c[i] + k[i])},表示前j个先用最优方案 买,然后第j+1~i个都用第i号的电源。答案为d[n]。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <cmath>

#define INF 0x3f3f3f3f

#define FRER() freopen("in.txt", "r", stdin);
#define FREW() freopen("out.txt", "w", stdout);

using namespace std;

struct lamp {
    int v, k, c, l;
    bool operator < (const lamp& temp) const {
        return v < temp.v;
    }
};

const int maxn = 1000 + 5;

lamp light[maxn];
int n, dp[maxn], s[maxn];

int main()
{
    // FRER();
    // FREW();
    while(cin >> n, n) {
        for(int i = 1; i <= n; ++i) 
            cin >> light[i].v >> light[i].k >> light[i].c >> light[i].l;
        sort(light + 1, light + n + 1);
        for(int i = 1; i <= n; ++i) 
            s[i] = s[i - 1] + light[i].l;

        for(int i = 1; i <= n; ++i) {
            dp[i] = s[i] * light[i].c + light[i].k;
            for(int j = 1; j < i; ++j) 
                dp[i] = min(dp[i], dp[j] + (s[i] - s[j])*light[i].c + light[i].k);
        }
        cout << dp[n] << endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/fan-jiaming/p/10059767.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值