TOJ 1158 Computers DP

た 入场券 2022-05-17 01:14 310阅读 0赞

1158: Computers

描述

Everybody is fond of computers, but buying a new one is always a money challenge. Fortunately, there is always a convenient way to deal with. You can replace your computer and get a brand new one, thus saving some maintenance cost. Of course, you must pay a fixed cost for each new computer you get.
Suppose you are considering an n year period over which you want to have a computer. Suppose you buy a new computer in year y, 1<=y<=n Then you have to pay a fixed cost c, in the year y, and a maintenance cost m(y,z) each year you own that computer, starting from year y through the year z, z<=n, when you plan to buy - eventually - another computer.
Write a program that computes the minimum cost of having a computer over the n year period.

输入

The program input is from a text file. Each data set in the file stands for a particular set of costs. A data set starts with the cost c for getting a new computer. Follows the number n of years, and the maintenance costs m(y,z), y=1..n, z=y..n. The program prints the minimum cost of having a computer throughout the n year period.
White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

输出

For each set of data the program prints the result to the standard output from the beginning of a line.

样例输入

3
3
5 7 50
6 8
10

样例输出

19

提示

An input/output sample is shown above. There is a single data set. The cost for getting a new computer is c=3. The time period n is n=3 years, and the maintenance costs are:

  • For the first computer, which is certainly bought: m(1,1)=5, m(1,2)=7, m(1,3)=50,
  • For the second computer, in the event the current computer is replaced: m(2,2)=6, m(2,3)=8,
  • For the third computer, in the event the current computer is replaced: m(3,3)=10

题意:每台电脑价钱一样,买一台要花费c元,DP取舍买新电脑,还是保养旧电脑,找出最小花费。

  1. #include <stdio.h>
  2. #include <algorithm>
  3. using namespace std;
  4. int sum[1005][1005];
  5. int main()
  6. {
  7. int c,n,i,j;
  8. while (scanf("%d",&c)!=EOF)
  9. {
  10. scanf("%d",&n);
  11. for(i=0;i<n;i++)
  12. {
  13. for(j=i;j<n;j++)
  14. scanf("%d",&sum[i][j]);
  15. }
  16. for(i=0;i<n;i++)
  17. {
  18. for(j=i;j<n;j++)
  19. {
  20. if(i==0)
  21. sum[i][j]=c+sum[i][j];
  22. else
  23. sum[i][j]=min(sum[i-1][i-1]+c+sum[i][j],sum[i-1][j]);
  24. }
  25. }
  26. printf("%d\n",sum[n-1][n-1]);
  27. }
  28. return 0;
  29. }

发表评论

表情:
评论列表 (有 0 条评论,310人围观)

还没有评论,来说两句吧...

相关阅读

    相关 hdu 1158(dp)

    题意: 一项工程需要n个月完成,每个月最少需要的员工数为a\[i\],雇佣一个员工和开除一个员工的费用为hire与fire,每个员工每个月的工资为salary,现在要求完成这

    相关 TOJ 4674 数塔II DP

    4674: 数塔II 描述 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少,并输出一条路径。 7 3 8