题意:共有n种(n<=1000)种灯泡,每种灯泡用4个数值表示。电压V(V<=132000),电源费用K(K<=1000),每个灯泡的费用C(C<=10)和所需灯泡的数量L(1<=L<=100)。把一些灯泡换成电压更高的另一种灯泡以节省电源的钱(不能换成电压更低的灯泡)。计算出最优方案费用。
分析:
1、每种电压的灯泡要么全换要么全不换,否则电压不同,需要买更多电源不划算。
2、把灯泡按电压从小到大排序。
3、sum[i]---前i种灯泡的总数量
4、dp[i]----灯泡1~i的最小开销
5、状态转移方程dp[i] = Min(dp[i], dp[j] + (sum[i] - sum[j]) * num[i].c + num[i].k);表示前j个先用最优方案买,第j+1~i个用第i号的电源。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 1000 + 10; const int MAXT = 10000 + 10; using namespace std; int sum[MAXN]; int dp[MAXN]; struct Node{ int v, k, c, l; void read(){ scanf("%d%d%d%d", &v, &k, &c, &l); } bool operator < (const Node& rhs)const{ return v < rhs.v; } }num[MAXN]; int main(){ int n; while(scanf("%d", &n) == 1){ if(!n) return 0; for(int i = 1; i <= n; ++i){ num[i].read(); } sort(num + 1, num + n + 1); sum[0] = 0; for(int i = 1; i <= n; ++i){ sum[i] = sum[i - 1] + num[i].l; } dp[0] = 0; for(int i = 1; i <= n; ++i){ dp[i] = INT_INF; for(int j = 0; j < i; ++j){ dp[i] = Min(dp[i], dp[j] + (sum[i] - sum[j]) * num[i].c + num[i].k); } } printf("%d\n", dp[n]); } return 0; }