ZCMU-1404-Computers

来源:互联网 发布:阿里丁丁 mac 编辑:程序博客网 时间:2024/05/21 06:40

1404: Computers

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 26  Solved: 15
[Submit][Status][Web Board]

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. All integers in the problem are bigger than 0 and no more than 1000.

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.
【解析】
一开始没看懂什么意思,后来才逐渐明白,输入的第一个数是购买电脑的花费,所以第一年肯定是需要买一台电脑先,然后下面一行是要使用几年也就是维修费当然还要继续加上买电脑的钱。求最小的花费,之前一直不明白的是为什么1,1,2,3这样就是三年了。2,3,就是可以理解成从第二年开始到第三年结束的时候这样就正好用了三年的,样例1也就是5+3+8+3,=19.加了两个三是因为要买电脑的花费,后面的8和5是维修电脑的花费,用dp就比较好做了。
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;int main(){    int n,i,p,j,m;    while(~scanf("%d",&n))    {        int a[1010][1010];        int b[1001];        scanf("%d",&m);        for(i=1;i<=m;i++)        {            for(j=i;j<=m;j++)            {                scanf("%d",&a[i][j]);            }        }        b[0]=0;        for(i=1;i<=m;i++)        {            b[i]=100000000;//先给它赋一个大值之后再比较            for(j=0;j<i;j++)            {                b[i]=min(b[i],b[j]+n+a[j+1][i]);//先算前面的最小花费再到后面            }        }        printf("%d\n",b[m]);    }    return 0;}

0 0
原创粉丝点击