POJ - 3486 Computers(DP)
2017-08-16 13:28
411 查看
Computers
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 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.
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..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.
Output
For each set of data the program prints the result to the standard output from the beginning of a line.
Sample Input
Sample Output
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)=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.
题意:给你一堆电脑的维护费用,和购买一台电脑的花费,求让你一直有电脑的最小费用,提示写的很清楚……
解题思路:很裸的DP,独立做完这道题对DP应该会更有感觉。首先定义一个数组dp[i]代表前i年所需的最小费用,根据决策无后效性原理,第i年的最小费用对后面的年份没有影响,所以对于每一年,只需遍历它前面的年份找到最小的那个,更新每一年即可,转移方程为dp[i]=min(dp[i],dp[j-1]+m[j][i]+C);意思是找到在第j-1年更新电脑所需的最小花费。(1<=j<=i)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <string.h>
#include <algorithm>
#include <queue>
#define INF 1<<29
using namespace std;
int C,N;
int m[10005][10005];
int dp[10005];
int main(){
while(~scanf("%d%d",&C,&N)){
for(int i=1;i<=N;i++)
for(int j=i;j<=N;j++)
scanf("%d",&m[i][j]);
//先初始化每一年为无限大的花费,这样才能找到最小
for(int i=1;i<=N;i++)
dp[i]=INF;
dp[0]=0;//没买电脑时花费为0,j-1有可能为0
dp[1]=m[1][1]+C;//第一年买电脑的花费肯定是这个
for(int i=2;i<=N;i++)
for(int j=1;j<=i;j++){
dp[i]=min(dp[i],dp[j-1]+m[j][i]+C);
}
printf("%d\n",dp
);
}
return 0;
}
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 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.
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..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.
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)=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.
题意:给你一堆电脑的维护费用,和购买一台电脑的花费,求让你一直有电脑的最小费用,提示写的很清楚……
解题思路:很裸的DP,独立做完这道题对DP应该会更有感觉。首先定义一个数组dp[i]代表前i年所需的最小费用,根据决策无后效性原理,第i年的最小费用对后面的年份没有影响,所以对于每一年,只需遍历它前面的年份找到最小的那个,更新每一年即可,转移方程为dp[i]=min(dp[i],dp[j-1]+m[j][i]+C);意思是找到在第j-1年更新电脑所需的最小花费。(1<=j<=i)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <string.h>
#include <algorithm>
#include <queue>
#define INF 1<<29
using namespace std;
int C,N;
int m[10005][10005];
int dp[10005];
int main(){
while(~scanf("%d%d",&C,&N)){
for(int i=1;i<=N;i++)
for(int j=i;j<=N;j++)
scanf("%d",&m[i][j]);
//先初始化每一年为无限大的花费,这样才能找到最小
for(int i=1;i<=N;i++)
dp[i]=INF;
dp[0]=0;//没买电脑时花费为0,j-1有可能为0
dp[1]=m[1][1]+C;//第一年买电脑的花费肯定是这个
for(int i=2;i<=N;i++)
for(int j=1;j<=i;j++){
dp[i]=min(dp[i],dp[j-1]+m[j][i]+C);
}
printf("%d\n",dp
);
}
return 0;
}
相关文章推荐
- poj 3486 Computers dp的思想
- POJ 3486 & HDU 1913 Computers(dp)
- POJ 3486 & HDU 1913 Computers(dp)
- poj 3186 Treats for the Cows(区间dp)
- poj-1080-形如下表dp
- poj 1837 dp有点类似背包
- poj 2096 概率dp
- poj 2385Apple Catching(简单dp)
- POJ 2486 Apple Tree(树形dp)
- 经典问题六.【二维的区间dp】棋盘分割 poj 1191
- poj 1417(并查集+简单dp)
- POJ 2018 Best Cow Fences + UVA Live 4726 Average 斜率优化DP
- [DP-LCS] POJ 1458
- POJ 3133 Manhattan Wiring(插头DP)
- poj 3071 概论 dp
- [DP-LIS] POJ 2533
- poj 2336 Ferry Loading II dp
- poj-1390-Blocks(方块消除)-DP
- poj1463(树形dp)
- POJ_1742 Coins(DP)