参考了这个
点击打开链接才会的题
刚开始写动归思维转不过来,中间的推理和证明没什么感觉。
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1000 + 5;
struct info {
int V;
int K;
int C;
int L;
};
info plan[MAXN];
int quantity[MAXN] = { 0 };//灯泡数量累计
int total_cost[MAXN] = { 0 };
//从小到大排序意味着每次替换都必须购买voltage
bool compare(info& a, info& b) {
return a.V <= b.V;
}
void quantity_ini(int n) {
quantity[0] = plan[0].L;
for (int i = 1; i < n; i++)
quantity[i] = quantity[i - 1] + plan[i].L;
}
int redesign(info a[], int n) {
quantity_ini(n);
// bool adopted[MAXN] = { 0 };//标记已经选用的voltage
for (int i = 0; i < n; i++) {
total_cost[i] = a[i].K + a[i].C*quantity[i];
for (int j = 1; j < i; j++) {//全i(即j = 0)的情况已经考虑过了
int temp = total_cost[j] + (quantity[i] - quantity[j])*a[i].C + a[i].K;
if (total_cost[i] > temp)
total_cost[i] = temp;
}
}
return 0;
}
int main() {
int n;
while (cin >> n && n != 0) {
for (int i = 0; i < n; i++)
cin >> plan[i].V >> plan[i].K >> plan[i].C >> plan[i].L;
sort(plan, plan + n, compare);
redesign(plan, n);
cout << total_cost[n-1] << endl;
}
return 0;
}