HDU 1158 dp

来源:互联网 发布:淘宝助理5.6.9 编辑:程序博客网 时间:2024/05/02 00:35

Employment Planning
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4246 Accepted Submission(s): 1776

Problem Description
A project manager wants to determine the number of the workers needed in every month. He does know the minimal number of the workers needed in each month. When he hires or fires a worker, there will be some extra cost. Once a worker is hired, he will get the salary even if he is not working. The manager knows the costs of hiring a worker, firing a worker, and the salary of a worker. Then the manager will confront such a problem: how many workers he will hire or fire each month in order to keep the lowest total cost of the project.

Input
The input may contain several data sets. Each data set contains three lines. First line contains the months of the project planed to use which is no more than 12. The second line contains the cost of hiring a worker, the amount of the salary, the cost of firing a worker. The third line contains several numbers, which represent the minimal number of the workers needed each month. The input is terminated by line containing a single ‘0’.

Output
The output contains one line. The minimal total cost of the project.

Sample Input
3
4 5 6
10 9 11
0

Sample Output
199

dp[i][j],i为月数,j为人数。对于每个月数枚举所有可能的人数。由上一个月数的人数dp得来。

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int dp[15][10000];int m[15];int main(){    int n;    while(scanf("%d",&n)){        if(n == 0)            break;        int h,c,f;        scanf("%d%d%d",&h,&c,&f);        int i,j,k,tem;        int Max = 0;        for(i = 1;i<=n;i++){            scanf("%d",&m[i]);            if(m[i]>Max)                Max = m[i];        }        for(i = m[1];i<=Max;i++)            dp[1][i] = (h+c)*i;        for(i = 2;i<=n;i++){            for(j = m[i];j<=Max;j++){                int Min = 0x3f3f3f3f;                for(k = m[i-1];k<=Max;k++){                    if(k>j)                        tem = dp[i-1][k] + f*(k-j) + c*j;                    else                        tem = dp[i-1][k] + h*(j-k) + c*j;                    if(tem<Min)                        Min = tem;                }                dp[i][j] = Min;            }        }        int MIN = 0x3f3f3f3f;        for(i = m[n];i<=Max;i++)            if(dp[n][i]<MIN)                MIN = dp[n][i];        printf("%d\n",MIN);    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刚出生的宝宝不吃母乳怎么办 宝宝六个月奶不够吃怎么办 六个月奶不够吃怎么办 刚出生奶不够吃怎么办 做梦醒了看见鬼怎么办 宝宝到陌生地方哭闹怎么办 大人生病住院小孩没人带怎么办 孕妇被小猫抓了怎么办 怀孕了家里有猫怎么办 厕所被湿纸巾堵了怎么办 5天新生儿不拉屎怎么办 4月宝宝不拉屎怎么办 两岁宝宝晚上睡觉哭闹怎么办 2月婴儿吐奶很多怎么办 心情不好回奶了怎么办 四个月了没奶怎么办 八岁宝宝还尿床怎么办 自己一人在家害怕怎么办 被猫抓伤流血了怎么办 被小狐狸咬了怎么办 怀孕吃了兔子肉怎么办 鸟总在窗子上啄怎么办? 有鸟飞进楼道里怎么办 租的房间房东要求改建怎么办 小偷偷钱抓住不还钱怎么办 损友圈羊被陌生人偷了怎么办 在酒店如果遇到客人偷东西怎么办 梦见家里有不好的东西怎么办 被小孩要破了怎么办啊 租一个房子小孩一进房就哭怎么办 墙缝里有蝙蝠窝怎么办 小蝙蝠在墙缝里怎么办 小孩一进屋就哭怎么办 屋门对着厕所门怎么办 入室门对厨房门怎么办 厕所正对入户门怎么办 小区楼交错冲路怎么办 床的位置在五鬼上怎么办 被甩了很痛苦怎么办 和对象想分手了怎么办 对象想跟你啪啪怎么办