POJ 3486 & HDU 1913 Computers(dp)

题目链接:PKU:HDU:

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

HDU:

pid=1913" target="_blank">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

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;
}
时间: 2024-10-13 00:08:20

POJ 3486 &amp; HDU 1913 Computers(dp)的相关文章

POJ 3486 &amp; 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 wi

HDU 4832 Chess (DP)

Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24    Accepted Submission(s): 10 Problem Description 小度和小良最近又迷上了下棋.棋盘一共有N行M列,我们可以把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王"在棋盘上的走法遵循十字路线.

HDU 2845 Beans (DP)

Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect the qualities, but everyo

HDU 4945 2048(dp)

题意:给n(n<=100,000)个数,0<=a[i]<=2048 .一个好的集合要满足,集合内的数可以根据2048的合并规则合并成2048 .输出好的集合的个数%998244353 . 比赛的时候想着1跟3可以合并成4 ....然后就越搞越复杂了.....2048玩得不多的我没有透彻的合并规则概念..... 看了题解写了发,妥妥地TLE...本地随意n=100,000都TLE了... 用dp[i][j]表示当前 i个2^j 的方案数 然后队友提醒优化,就是,当枚举到比如,value =

【POJ 1191】 棋盘分割(DP)

[POJ 1191] 棋盘分割(DP) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13811   Accepted: 4917 Description 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘.(每次切割都只能沿着棋盘格子的边进行) 原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分

hdu 1300 Pearls (dp)

题目大意: 多种珍珠,每次选购都要在原有的数量上加上10. 例如:买5个单价是10的珍珠.需要的花费是(5+10)*10= 150.买100个单价是20的珍珠 需要的花费是(100+10)*20= 2200.总共需要的花费是150+2200=2350.如果把珍珠的质量提高了.需要的105个 珍珠都买单价是20的.也就是说都买质量好的.总的花费是(5+100+10)*20= 2300.在两组数据看来.珍珠都 买了高品质的了,而且花费也少了! 问题是怎么样能花费最少买珍珠! 思路分析: dp [i]

poj 3267 The Cow Lexicon (dp)

链接:poj 3267 题意:给定一个主串,和单词序列,问最少在主串删除多少字母, 可以使其匹配到单词序列,如 browndcodw cow milk white black brown farmer 删除主串中的两个d,brown和cow就与整个主串匹配了 分析:dp[i]表示从主串中第i个字符开始,到第L个字符(结尾处) 这段区间最少要删除的字符数, 则状态转移方程为: dp[i]=dp[i+1]+1  不能匹配时 dp[i]=min(dp[i],dp[pos]+pos-i-m)  可以匹配

hdu 2845 Beans(DP)

题意: M*N的矩阵,每个格子上有一个值. 规则:如果你拾起了某个格子(i,j)上的值,那么第i-1行.第i+1行.(i,j-1)格子上.(i,j+1)格子上的值都不能取. 问最多可以取得多少值(最大值). 思路: 如果某行取了某一个值,则它的前一行和后一行都不能取.所以我们必须知道这行可以取得的最大值是多少. dp[i]=max( dp[i-1],dp[i-2]+a[i] ) dp[i]:前i个数能获得的最大值.第i个可以取,可以不取. 当每行的dp[N]都算出来后,可以发现从行的角度看,dp

poj - 1953 - World Cup Noise(dp)

题意:n位长的01序列(0 < n < 45),但不能出现连续的两个1,问序列有多少种. 题目链接:http://poj.org/problem?id=1953 -->>设dp[i][j]表示前 i 位中第 i 位为 j 时的序列数,则状态转移方程为: dp[i][0] = dp[i - 1][0] + dp[i - 1][1]; dp[i][1] = dp[i - 1][0]; 因为对于相同的n,其结果是固定的,所以可以对一个n只计算一次,然后记住她.. #include <