码迷,mamicode.com
首页 > 其他好文 > 详细

照明系统设计

时间:2018-05-17 23:23:54      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:soft   type   amp   opera   col   pre   const   name   \n   

题意  设计某个地方的照明系统  一共需要n种不同类型的灯泡  接着输入 每种灯泡的电压v  对应电压电源的价格k  每个灯泡的价格c   需要这种灯泡的数量l   电压低的灯泡可以用电压高的灯泡替换   每种灯泡只需要一个对应的电源   求完成这个照明系统的最少花费

 

Sample Input

3

100 500 10 20

120 600 8 16

220 400 7 18

0

Sample Output

778

 

按电压大小排序,s[i]为前i个灯泡的总数,d[i]为买前i种灯泡的最小花费,则:

                d[i]=min { d[j]+( s[i]-s[j] )*c[i]+k[i] }      表示前j个用最优方案买,j+1到 i都用i号电源。

输出d[n]即可。

 

技术分享图片
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long

const int maxn=1000+5;

int n,dp[maxn],s[maxn];

struct node{
    int v,k,c,l;
    bool operator<(const node &p) const {return v<p.v;}
}a[maxn];

template<typename T>void read(T& aa) {
    char cc; ll ff;aa=0;cc=getchar();ff=1;
    while((cc<0||cc>9)&&cc!=-) cc=getchar();
    if(cc==-) ff=-1,cc=getchar();
    while(cc>=0&&cc<=9) aa=aa*10+cc-0,cc=getchar();
    aa*=ff;
}

int main(){
    while(scanf("%d",&n)==1&&n){
        memset(a,0,sizeof(a));
        memset(dp,127,sizeof(dp));
        memset(s,0,sizeof(s));
        for(int i=1;i<=n;i++){
            read(a[i].v);read(a[i].k);read(a[i].c);read(a[i].l);
        }
        sort(a+1,a+1+n);
        for(int i=1;i<=n;i++){
            s[i]=s[i-1]+a[i].l;
            dp[i]=a[i].k+a[i].c*s[i];
            for(int j=1;j<=i;j++)
            dp[i]=min(dp[i],dp[j]+(s[i]-s[j])*a[i].c+a[i].k);
        }
        printf("%d\n",dp[n]);
    }
    return 0;
}
View Code

 

照明系统设计

标签:soft   type   amp   opera   col   pre   const   name   \n   

原文地址:https://www.cnblogs.com/rlddd/p/9053711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!