标签:uva 11400 lighting system desi dp
// uva 11400 // 首先得先明白,灯泡要么不换,要么全换,因为部分换的话 // 则还要额外花费电源的钱 // dp[i]表示前i种灯泡用最优策略所花费的最小开销 // 因为灯泡的电压只能高不能低,先按灯泡的电压升序排列 // 状态转移方程: // dp[i] = min(dp[i],dp[j]+(s[i]-s[j])*c[i]+k[i]); // s[i]表示前i种灯泡的总的数量 // c[i]表示第i种灯泡的数量 // k[i]表示第i种灯泡的电源费用 // dp[j]+(s[i]-s[j])*c[i]+k[i]表示把j+1到i全部换成i所要的开销 // 取个最小值就ok啦 // 注意一下初始化问题 // dp[i] = s[i]*c[i]+k[i]; // 哎,继续练吧。。。。 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <vector> #define ceil(a,b) (((a)+(b)-1)/(b)) #define endl '\n' #define gcd __gcd #define highBit(x) (1ULL<<(63-__builtin_clzll(x))) #define popCount __builtin_popcountll typedef long long ll; using namespace std; const int MOD = 1000000007; const long double PI = acos(-1.L); template<class T> inline T lcm(const T& a, const T& b) { return a/gcd(a, b)*b; } template<class T> inline T lowBit(const T& x) { return x&-x; } template<class T> inline T maximize(T& a, const T& b) { return a=a<b?b:a; } template<class T> inline T minimize(T& a, const T& b) { return a=a<b?a:b; } const int maxn = 1008; struct node{ int v; int k; int c; int num; }b[maxn]; int s[maxn]; int n; int dp[maxn]; bool cmp(node a,node b){ return a.v<b.v; } void init(){ for (int i=1;i<=n;i++){ scanf("%d%d%d%d",&b[i].v,&b[i].k,&b[i].c,&b[i].num); } sort(b+1,b+n+1,cmp); memset(dp,0x3f,sizeof(dp)); } void print(){ for (int i=0;i<n;i++) printf("%d ",dp[i]); puts(""); } void solve(){ s[0]=0; for (int i=1;i<=n;i++){ s[i] = s[i-1]+b[i].num; } dp[0]=0; for (int i=1;i<=n;i++) dp[i] = s[i]*b[i].c+b[i].k; for (int i=1;i<=n;i++){ for (int j=1;j<=i;j++) dp[i] = min(dp[i],dp[j]+(s[i]-s[j])*b[i].c+b[i].k); } //print(); printf("%d\n",dp[n]); } int main() { //freopen("G:\\Code\\1.txt","r",stdin); while(scanf("%d",&n)!=EOF){ if (!n) break; init(); solve(); } return 0; }
uva 11400 Lighting System Design dp
标签:uva 11400 lighting system desi dp
原文地址:http://blog.csdn.net/timelimite/article/details/45176275