T(1158: 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 y, 1<=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 z, z<=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.

输入

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…n, z=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.

输出

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

样例输入

3
3
5 7 50
6 8
10

样例输出

19

提示

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

  • 有一说一这题看了好久没看懂什么意思
    我觉得数据应该看成这样
    3 //表示电脑的单价,每次你要换电脑时都要加上这个单价
    3 //要用几年
    5 7 50 //有一台电脑用3年第一年维修费为5,第二年为7,第三年为50
     6 8 //用的第一年为6,第二年为8
      10
    这里最省的方法是3+5+8+3=19;
    理解为第一年用3买了电脑,第一年的维修费为5,第二年换了一台电脑+3,用俩年维修费为8,就算出来了;
    就是题目太抽象了。
#include<iostream>
#include<cstdio>
using namespace std;
int num,n,i,j,a[1001][1001];
int main()
{
	while(scanf("%d",&num)!=EOF)
	{
		cin>>n;
		for(i=0;i<n;i++)
		{
			for(j=i;j<n;j++)
			cin>>a[i][j];
		}
		for(i=0;i<n;i++)
		{
			for(j=i;j<n;j++)
			{
				if(i==0)
				a[i][j]+=num; //将i=0的位置都加num  例 8 10 53
				else 
				a[i][j]=min(a[i-1][i-1]+a[i][j]+num,a[i-1][j]);
			}
		}
		printf("%d\n",a[n-1][n-1]);
	}
} 

这个方法相对会好理解点,但时间复杂度挺高的,差点就超时了
 
 在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lioushuishoushou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值