其他分享
首页 > 其他分享> > (UVA - 11400)Lighting System Design(DP)

(UVA - 11400)Lighting System Design(DP)

作者:互联网

链接: https://vjudge.net/problem/UVA-11400

分析:先按电压从小到大排序,预处理求出1到i的数量和s[i],然后设dp[i] : 灯泡1到i的最小费用
状态转移方程: dp[0]=0;
dp[i]=min(dp[i],dp[j]+p[i].c*(s[i]-s[j])+p[i].k),j

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
const int INF=0x3f3f3f3f;
const int N=1e3+5;
struct Node
{
    int v,k,c,l;
    bool operator < (const Node& m)const
    {
        return v<m.v;
    }
} p[N];
int s[N],dp[N];
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1; i<=n; i++)
            scanf("%d%d%d%d",&p[i].v,&p[i].k,&p[i].c,&p[i].l);
        sort(p+1,p+n+1);
        s[0]=0;
        for(int i=1; i<=n; i++)
            s[i]=s[i-1]+p[i].l;
        mem(dp,INF);
        dp[0]=0;
        for(int i=1; i<=n; i++)
            for(int j=0; j<i; j++)
            {
                dp[i]=min(dp[i],dp[j]+p[i].c*(s[i]-s[j])+p[i].k);
  //              printf("%d %d %d %d\n",p[i].c,p[i].k,s[i],s[j]);
   //             printf("dp[%d]=%d\n",i,dp[i]);
            }
        printf("%d\n",dp[n]);
    }
    return 0;
}

 

标签:Node,const,11400,int,System,Design,include,dp
来源: https://blog.51cto.com/u_13696685/2990222