Lighting System Design UVA - 11400 动态规划
Posted fan-jiaming
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; }
以上是关于Lighting System Design UVA - 11400 动态规划的主要内容,如果未能解决你的问题,请参考以下文章
UVa11400 - Lighting System Design——[动态规划]
UVA11400 Lighting System Design