杭电1158

Employment Planning

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

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表示前i个月,j表示这个月使用的工人。
dp是个累加值,j的范围是从本月的最少工人数到所有月中最大的工人数,
核心做法就是将本月的最低人数到最多人数依次与上个月的最低人数到最高人数
做运算,求出dp[i][j].
 
代码:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 
 5 int dp[1000][1000];
 6 
 7 int getmax(int m, int n){
 8     return m > n ? m : n;
 9 }
10 int getmin(int m, int n){
11     return m > n ? n : m;
12 }
13 
14 int main(){
15     int n, i, j, k, money[3], people[1000], sum, min, maxnum;
16     while(scanf("%d", &n) && n){
17         //getchar();
18         for(i = 0; i < 3; i ++){
19             scanf("%d", &money[i]);
20         }
21         maxnum = -1;
22         for(i = 0; i < n; i ++){
23             scanf("%d", &people[i]);
24             maxnum = getmax(maxnum, people[i]);
25         }
26         //printf("sdh");
27         memset(dp, 0, sizeof(dp));
28         for(i = people[0]; i <= maxnum; i ++){
29             dp[1][i] = (money[0] + money[1]) * i;
30         }
31         for(i = 2; i <= n; i ++){
32             for(j = people[i - 1]; j <= maxnum; j ++){
33                 min = 0xfffffff;
34                 for(k = people[i - 2]; k <= maxnum; k ++){
35                     if(k <= j){
36                         min = getmin(min, dp[i - 1][k] + money[0] * (j - k) + j * money[1]);
37                     }
38                     else{
39                         min = getmin(min, dp[i - 1][k] + money[2] * (k - j) + j * money[1]);
40                     }
41                 }
42                 dp[i][j] = min;
43             }
44         }
45         min = 0xfffffff;
46         for(i = people[n - 1]; i <= maxnum; i ++){
47             min = getmin(min, dp[n][i]);
48         }
49         printf("%d\n", min);
50     }
51     return 0;
52 }
View Code

 

posted @ 2014-07-14 16:51  小叶叶  阅读(190)  评论(0编辑  收藏  举报