POJ - 3486 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 y1<=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 zz<=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.

Input

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..nz=y..n. The program prints the minimum cost of having a computer throughout the nyear period.

White spaces can occur freely in the input. The input data are correct and terminate with an end of file.

Output

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

Sample Input

3
3
5 7 50
6 8
10

Sample Output

19

Hint

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)=5m(1,2)=7m(1,3)=50,
  • For the second computer, in the event the current computer is replaced: m(2,2)=6m(2,3)=8,
  • For the third computer, in the event the current computer is replaced: m(3,3)=10.

思路:用dp[i]代表前i天的最小花费,则对于每个j >= i, dp[j] = min(dp[j], dp[i - 1] + c + cost[i][j - i + 1])。所以暴力跑个n^2的dp就行了。

 1 #include <iostream>
 2 #include <fstream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <string>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #include <stack>
12 #include <vector>
13 #include <set>
14 #include <map>
15 #include <list>
16 #include <iomanip>
17 #include <cctype>
18 #include <cassert>
19 #include <bitset>
20 #include <ctime>
21 
22 using namespace std;
23 
24 #define pau system("pause")
25 #define ll long long
26 #define pii pair<int, int>
27 #define pb push_back
28 #define mp make_pair
29 #define clr(a, x) memset(a, x, sizeof(a))
30 
31 const double pi = acos(-1.0);
32 const int INF = 0x3f3f3f3f;
33 const int MOD = 1e9 + 7;
34 const double EPS = 1e-9;
35 
36 /*
37 #include <ext/pb_ds/assoc_container.hpp>
38 #include <ext/pb_ds/tree_policy.hpp>
39 
40 using namespace __gnu_pbds;
41 tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> T;
42 */
43 
44 int c, n, cost[5015][5015], dp[5015];
45 int main() {
46     while (~scanf("%d%d", &c, &n)) {
47         for (int i = 1; i <= n; ++i) {
48             for (int j = 1; j <= n - i + 1; ++j) {
49                 scanf("%d", &cost[i][j]);
50             }
51         }
52         clr(dp, INF);
53         dp[0] = 0;
54         for (int i = 1; i <= n; ++i) {
55             for (int j = i; j <= n; ++j) {
56                 dp[j] = min(dp[j], dp[i - 1] + c + cost[i][j - i + 1]);
57             }
58         }
59         printf("%d\n", dp[n]);
60     }
61     return 0;
62 }

 

转载于:https://www.cnblogs.com/BIGTOM/p/8492923.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值