zoukankan      html  css  js  c++  java
  • Employment Planning DP

    Employment Planning

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 4782    Accepted Submission(s): 2019


    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[i][j] = min{dp[i-1][k] + cost[i][j]},其中cost[i][j]是第i月的花费,
    1~当k<=j时,第i个月请了人所以cost[i][j] = j*salary + (j-k)*hire
    2~当k>j时,第i个月炒了人,所以cost[i][j] = j*salary + (k-j)*fire
     
    输入时记录了最多需要的人数。
     
    因为他给的是每个月最少需要的人数,所以for(该月需要的最少人数——max_people)而不是for(1——该月需要的最少人数)
     
    直接初始化第一个月。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 const int INF = 0x3f3f3f3f;
     7 int dp[15][100];
     8 int people[15];
     9 void solve(){
    10     int n;
    11     int hire,salary,fire;
    12     while(scanf("%d",&n) == 1&&n){
    13         scanf("%d%d%d",&hire,&salary,&fire);
    14         int max_people = 0;
    15         for(int i = 1; i<=n; i++){
    16              scanf("%d",&people[i]);
    17              max_people = max(max_people,people[i]);
    18         }
    19       //  memset(dp,INF,sizeof(dp));
    20         for(int i = people[1]; i<=max_people; i++){
    21             dp[1][i] = (hire+salary)*i;
    22         }
    23         for(int i = 2; i<=n; i++){
    24             for(int j = people[i]; j<=max_people; j++){
    25                     int minn = INF;
    26                 for(int k = people[i-1]; k<=max_people; k++){
    27                         int cost = k<=j?(salary*j+(j-k)*hire):(salary*j+(k-j)*fire);
    28                         minn =min(minn,(dp[i-1][k] + cost));
    29                 }
    30                 dp[i][j] = minn;
    31             }
    32         }
    33         int ans = INF;
    34         for(int j = people[n]; j<=max_people; j++) ans = min(ans,dp[n][j]);
    35         printf("%d
    ",ans);
    36     }
    37 }
    38 int main()
    39 {
    40     solve();
    41     return 0;
    42 }
  • 相关阅读:
    web 单例 多例
    python socket客户端
    foy: 轻量级的基于 nodejs 的通用 build 工具
    Hydux: 一个 Elm-like 的 全功能的 Redux 替代品
    AlarmManager使用注意事项
    【转】android ListView 几个重要属性
    自己写的小工具软件集合
    win8.1 cygwin编译java轻量虚拟机avian
    android 图片缩放抗锯齿
    windows phone和android,ios的touch事件兼容
  • 原文地址:https://www.cnblogs.com/littlepear/p/5396074.html
Copyright © 2011-2022 走看看