zoukankan      html  css  js  c++  java
  • POJ

    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
    ", dp[n]);
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    Java多线程 编写三各类Ticket、SaleWindow、TicketSaleCenter分别代表票信息、售票窗口、售票中心。 售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟此售票过程。
    java中异常处理机制 throw抛出自定义业务逻辑异常 throws继续抛出 catch捕获后会自动继续抛向调用方法
    Map集合应用 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)...
    Java中List集合排序的方法 比较器的使用 根据学生对象数学 语文 英语成绩总和进行sort排序
    美国银行
    Time Difference
    马来西亚与新加坡两国的标准时间为UTC+8
    java主要城市时区对照表
    韩国时区 KST
    AIX 系统
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/8492923.html
Copyright © 2011-2022 走看看