首先是一個貪心,一種燈泡要麼全都換,要麼全都不換。c++
先排序,定義狀態d[i]爲前面i種燈泡的最小花費,狀態轉移就是從d[j],j<i,加上 i前面的j+1到i-1種燈泡換成i的花費。spa
下標排序玩脫了。。。code
#include<bits/stdc++.h> using namespace std; const int maxn = 1e3+3; int V[maxn], K[maxn], C[maxn], L[maxn]; int d[maxn],r[maxn],s[maxn]; bool cmp(int a,int b) { return V[a]<V[b]; } int main() { //freopen("in.txt","r",stdin); int n; s[0] = 0; while(scanf("%d",&n),n){ for(int i = 1; i <= n; i++) { scanf("%d%d%d%d",V+i,K+i,C+i,L+i); r[i] = i; } sort(r+1,r+1+n,cmp); for(int id = 1; id <= n; id++){ int i = r[id]; s[id] = s[id-1] + L[i]; d[id] = s[id]*C[i] + K[i]; for(int id2 = 1; id2 < id; id2++){ int j = r[id2]; d[id] = min(d[id],d[id2]+(s[id]-s[id2])*C[i]+K[i]); } } printf("%d\n",d[n]); } return 0; }