题意:
给定n(n≤1000)n(n≤1000) 种类型灯泡,每个灯泡给出其电压v(v≤132000)v(v≤132000) ,电源花费k(k≤1000)k(k≤1000) ,每个灯的花费c(c≤10)c(c≤10) 和需求量l(1≤l≤100)l(1≤l≤100) 。现在通过用电压大的灯泡替换某些电压小的灯泡来减小总花费,求最小的花费。
思路:
每种灯泡要么不换要么全换 ,按电压从小到大对灯泡进行排序,设s[i](1<=i<=n)为前i种灯泡的和,d[i](1<=i<=n)为前n种灯泡最小的花费,d[i] = min{d[j]+(s[i]-s[j])*c[i]+k[i]},表示前j个用最优方案买,第j+1~i个都用i号电源,答案为d[n]。
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<sstream>
#include<cctype>
#pragma GCC optizmize(3)
using namespace std;
const int N = 1010;
const int INF = 0x3f3f3f3f;
struct bulb {
int v, k, c, l;
}b[N];
bool cmp(bulb x, bulb y) {
return x.v < y.v;
}
int main() {
int n;
while(cin>>n && n) {
for(int i=1; i<=n; i++)
cin>>b[i].v>>b[i].k>>b[i].c>>b[i].l;
sort(b+1, b+n+1, cmp);
int s[N], d[N];
s[0] = 0;
for(int i=1; i<=n; i++)
s[i] = s[i-1] + b[i].l;
memset(d, INF, sizeof(d));
d[0] = 0;
for(int i=1; i<=n; i++)
for(int j=0; j<i; j++)
d[i] = min(d[j] + (s[i] - s[j]) * b[i].c + b[i].k, d[i]);
cout<<d[n]<<endl;
}
}