Interesting Calculator

Time Limit: 2 Sec  Memory Limit: 128 MB
Submit: 163  Solved: 49

Description

There is an interesting calculator. It has 3 rows of buttons.





 





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

AC代码例如以下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define M 10010
using namespace std; struct H
{
friend bool operator <(const H x,const H y)
{
return x.a>y.a||(x.a==y.a&&x.b>y.b);
}
int a,b,c; }; int cas=0;
int n,m;
int r1[10],r2[10],r3[10];
int vis[100005]; void bfs()
{
int i,j;
H aa,bb,cc;
priority_queue<H> q;
aa.c=n;
aa.a=0;
aa.b=0;
q.push(aa);
while(!q.empty())
{
bb=q.top();
q.pop();
if(vis[bb.c])
continue;
vis[bb.c]=1;
if(bb.c==m)
{
printf("Case %d: %d %d\n",cas,bb.a,bb.b);
return;
}
for(i=0;i<10;i++)
{
cc.c=bb.c*10+i;
if(cc.c>=0&&cc.c<100001&&!vis[cc.c])
{
cc.b=bb.b+1;
cc.a=bb.a+r1[i];
q.push(cc);
}
cc.c=bb.c+i;
if(cc.c>=0&&cc.c<100001&&!vis[cc.c])
{
cc.b=bb.b+1;
cc.a=bb.a+r2[i];
q.push(cc);
}
cc.c=bb.c*i;
if(cc.c>=0&&cc.c<100001&&!vis[cc.c])
{
cc.b=bb.b+1;
cc.a=bb.a+r3[i];
q.push(cc);
}
}
}
} int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
cas++;
for(i=0;i<10;i++)
scanf("%d",&r1[i]);
for(i=0;i<10;i++)
scanf("%d",&r2[i]);
for(i=0;i<10;i++)
scanf("%d",&r3[i]);
memset(vis,0,sizeof vis); bfs();
}
return 0;
}

最新文章

  1. 【WCF】为终结点地址应用地址头
  2. 将上传图片转成base64(转)
  3. MyEclipse10安装SVN插件
  4. hbase体系结构以及说明
  5. java定时器,Spring定时器和Quartz定时器
  6. 14.4.5 System Tablespace 系统表空间
  7. html(三)
  8. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅲ
  9. Python成长之路第二篇(2)_列表元组内置函数用法
  10. 阿里云ECS每天一件事D8:nginx1.7整合php5.4
  11. 使用Idea作为go的IDE
  12. MyBatis 延迟加载
  13. IDEA第八章----远程调试
  14. rails项目如何改变已建立的model结构
  15. MTD下的Nand驱动
  16. Windows netstat 查看端口、进程占用 查看进程路径
  17. 洛谷P2699小浩的幂次运算
  18. WebApi 序列化 循环引用问题
  19. python+selenium十三:破解简单的图形验证码
  20. Ajax同步

热门文章

  1. 微信小程序------微信支付模块
  2. jquery 应用
  3. JAVA基础——生产者消费者问题
  4. MATLAB优化——减少for的使用
  5. 源码学习-Object类
  6. Lucene实现全文检索的流程
  7. 动态规划之最长递增子序列(LIS)
  8. android开发里跳过的坑——onActivityResult在启动另一个activity的时候马上回调
  9. cdq分治入门--BZOJ1176: [Balkan2007]Mokia
  10. 【IntelliJ】IDEA使用--字体、编码和基本设置