zoukankan      html  css  js  c++  java
  • POJ 3486 & HDU 1913 Computers(dp)

    题目链接:PKU:HDU:

    PKU:http://poj.org/problem?id=3486

    HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1913


    Description

    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 n year 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.

    Source


    题意:

    有个人想在不同的时期分别买一台新电脑,使用这些电脑N年。

    每台电脑每年都有维护费用,每买一台电脑时。都要花固定的成本C。

    求出使用N年的最少钱是多少。

    PS:

    dp分段更新从第一年到第n年的过程中每一年的值。


    代码例如以下:

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    int mp[1017][1017];
    int main()
    {
        int c, y;
        while(scanf("%d",&c)!=EOF)
        {
            scanf("%d",&y);
            for(int i = 1; i <= y; i++)
            {
                for(int j = i; j <= y; j++)
                {
                    scanf("%d",&mp[i][j]);
                }
            }
    
            for(int i = 1; i <= y; i++)
            {
                for(int j = 1; j <= i; j++)
                {
                    mp[1][i] = min(mp[1][j-1]+mp[j][i]+c,mp[1][i]);
                }
            }
            printf("%d
    ",mp[1][y]+c);
        }
        return 0;
    }
    



  • 相关阅读:
    C#中如何只保留小数点后面两位?
    Int16 Int32 Int64
    移动端重构系列3——重置样式
    移动端重构系列2——整体布局(转载)
    移动端重构系列1——新建空白页面(转载)
    八种创建等高列布局(转载)
    一个完整的Flexbox指南(转载)
    等高列布局、水平垂直居中与置顶页脚(转载)
    Block formatting context & Inline formatting context(BFC&IFC)的区别(转载)
    [转载]网页动画的十二原则
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/7258730.html
Copyright © 2011-2022 走看看