POJ 3486 & HDU 1913 Computers(dp)

来源:互联网 发布:mysql user host 编辑:程序博客网 时间:2024/05/08 02:07

题目链接: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

335 7 506 810

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

Southeastern Europe 2007


题意:

有个人想在不同的时期分别买一台新电脑,使用这些电脑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\n",mp[1][y]+c);    }    return 0;}



1 0
原创粉丝点击