先上题目:

12664 Interesting Calculator
There is an interesting calculator. It has 3 rows of button.
• Row 1: button 0, 1, 2, 3, . . . , 9. Pressing each button appends that digit to the end of the display.
• Row 2: button +0, +1, +2, +3, . . . , +9. Pressing each button adds that digit to the display.
• Row 3: button *0, *1, *2, *3, . . . , *9. Pressing each button multiplies that digit to the display.
Note that it never displays leading zeros, so if the current display is 0, pressing 5 makes it 5 instead
of 05. If the current display is 12, you can press button 3, +5, *2 to get 256. Similarly, to change the
display from 0 to 1, you can press 1 or +1 (but not both!).
Each button has a positive cost, your task is to change the display from x to y with minimum cost.
If there are multiple ways to do so, the number of presses should be minimized.
Input
There will be at most 30 test cases. The first line of each test case contains two integers x and y
(0 ≤ x ≤ y ≤ 105
). Each of the 3 lines contains 10 positive integers (not greater than 105
), i.e. the
costs of each button.
Output
For each test case, print the minimal cost and the number of presses.
Sample Input
12 256
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
12 256
100 100 100 1 100 100 100 100 100 100
100 100 100 100 100 1 100 100 100 100
100 100 10 100 100 100 100 100 100 100
Sample Output
Case 1: 2 2
Case 2: 12 3

  题意:给出一个数x和一个数y,有30种操作,每种操作需要付出一定的代价,问你从x变成y最少需要多少代价。

  DP,想法是从小到大枚举每一个可能变成的数,然后枚举不同操作会变成的值,保存变成这个值的最小代价以及步数,最终推导到目标数就可以了。

  这一题需要注意的东西是:①在最小代价的基础上还需要最少步数,②有可能先变成0再变成其他数的路径所需要的代价更短。

  所以我们一开始可以初始化dp[0]=乘上0的代价,同时把步数变成一,然后才初始化dp[x]=0,步数为0,然后从小到大扫描就可以了。

上代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 100002
#define ll long long
#define INF (((ll)1)<<60)
using namespace std; ll dp[MAX],e;
int cnt[MAX];
int x,y; ll cost[][]; int main()
{
int t;
//freopen("data.txt","r",stdin);
t=;
while(scanf("%d %d",&x,&y)!=EOF){
for(int i=;i<;i++) for(int j=;j<;j++) scanf("%lld",&cost[i][j]);
for(int i=;i<=y;i++){
dp[i]=INF; cnt[i]=;
}
if(x!=){dp[]=cost[][]; cnt[]=;}
dp[x]=;
for(int i=;i<=y;i++){
for(int j=;j<;j++){
e=i*+j;
if(e<=y){
ll u=dp[i]+cost[][j];
if(dp[e]>u){
dp[e]=u;
cnt[e]=cnt[i]+;
}else if(dp[e]==u && cnt[e]>cnt[i]+){
cnt[e]=cnt[i]+;
}
}
}
for(int j=;j<;j++){
e=i+j;
if(e<=y){
ll u=dp[i]+cost[][j];
if(dp[e]>u){
dp[e]=u;
cnt[e]=cnt[i]+;
}else if(dp[e]==u && cnt[e]>cnt[i]+){
cnt[e]=cnt[i]+;
}
}
}
for(int j=;j<;j++){
e=(ll)i*j;
if(e<=y){
ll u=dp[i]+cost[][j];
if(dp[e]>u){
dp[e]=u;
cnt[e]=cnt[i]+;
}else if(dp[e]==u && cnt[e]>cnt[i]+){
cnt[e]=cnt[i]+;
}
}
}
}
printf("Case %d: %lld %d\n",t++,dp[y],cnt[y]); }
return ;
}

/*12664*/

最新文章

  1. 一点做用户画像的人生经验(一):ID强打通
  2. String相关的问题
  3. ffmpeg relocation error
  4. MSSQLSERVER服务无法启动的解决方案
  5. 用grunt进行前端工程化之路
  6. [troubleshoot][archlinux][bcache] 修改linux文件系统 / 分区方案 / 做混合硬盘 / 系统转生大!手!术!(调整底层架构,不!重!装!)
  7. linux文件描述符--转载
  8. Python 基础语法(二)
  9. FastJson的基本用法----转
  10. char[] 操作
  11. HDU 4941 Magical Forest 【离散化】【map】
  12. Oracle基础学习1--Oracle安装
  13. H TML5 之 (2) 小试牛刀
  14. T - 阿牛的EOF牛肉串(第二季水)
  15. MATLAB中return和break
  16. ubuntu14.04中 gedit 凝视能显示中文,而source insight中显示为乱码的解决的方法
  17. web缓存之--http缓存机制
  18. Java与.net的选择和比较
  19. seq2seq笔记
  20. 原生js点击按钮切换图片

热门文章

  1. Nginx的alias与root的用法区别和location匹配规则
  2. PCB CAM自动化程序协同业务流
  3. hibernate基础简单入门1---helloword
  4. js判断ie6的代码
  5. List 序列化
  6. Aspose Cells dll 实现数据简单下载
  7. C#模拟百度登录并到指定网站评论回帖(一)
  8. 9.Hierarchy Editor
  9. python--9、进程池
  10. jQuery——节点操作