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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3fffffff
using namespace std;
struct Node
{
int time;
int cost;
int value;
friend bool operator < (Node a,Node b)
{
if(a.cost==b.cost)
return a.time>b.time;
return a.cost>b.cost;
}
}node;
priority_queue<Node> q;
int val[];
int mp[][];
int cas=;
int x,y,i,j; void bfs()
{
node.time=;
node.cost=;
node.value=x;
while(!q.empty()) q.pop();
q.push(node);
val[x]=;
while(!q.empty())
{
Node tp,tmp=q.top();
q.pop();
if(tmp.value==y)
{
printf("Case %d: %d %d\n",cas++,tmp.cost,tmp.time);
return ;
}
for(int i=;i<;i++)
{
for(int j=;j<=;j++)
{
if(i==)
tp.value=tmp.value*+j;
else if(i==)
tp.value=tmp.value+j;
else
tp.value=tmp.value*j; tp.cost=tmp.cost+mp[i][j];
tp.time=tmp.time+; if(tp.value<=y&&tp.cost<val[tp.value])
{
q.push(tp);
val[tp.value]=tp.cost;
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&x,&y))
{
for(int i=;i<;i++)
val[i]=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
scanf("%d",&mp[i][j]);
}
}
bfs();
}
return ;
}
												

D - Interesting Calculator 【数值型BFS+优先队列】的更多相关文章

  1. G - Rescue 【地图型BFS+优先队列(有障碍物)】

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...

  2. E - A strange lift 【数值型BFS+上下方向】

    There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...

  3. C - 你经历过绝望吗?两次! 【地图型BFS+优先队列(障碍物)】

    4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的“猪坚强”.不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川“猪坚 ...

  4. 中南大学oj:1336: Interesting Calculator(广搜经典题目)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 There is an interesting calculator. It has 3 r ...

  5. CSU-1336: Interesting Calculator,最短路思想!

    1336: Interesting Calculator 这道题被LZQ抢了一血,于是去读题发现题意不难,纯广搜结果写的一塌糊涂. 题意:给你两个数x,y.由x每次可以经过一系列操作变换,每个变换都有 ...

  6. 湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator

    Interesting Calculator Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 163  Solved: 49 Description T ...

  7. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  8. heap表按字符串和数值型排序规则

    SQL> create user scan identified by scan default tablespace users; User created. SQL> grant db ...

  9. Swift编程语言学习1.4——数值型字面量、数值类型转换

    数值型字面量 整数字面量能够被写作: 一个十进制数,没有前缀 一个二进制数,前缀是0b 一个八进制数,前缀是0o 一个十六进制数,前缀是0x 以下的全部整数字面量的十进制值都是17: let deci ...

随机推荐

  1. 【题解】NOI2009二叉查找树 + NOIP2003加分二叉树

    自己的思维能力果然还是太不够……想到了这棵树所有的性质即中序遍历不变,却并没有想到怎样利用这一点.在想这道题的过程中走入了诸多的误区,在这里想记录一下 & 从中吸取到的教训(原该可以避免的吧) ...

  2. 【NOIP模拟赛】Drink 二维链表+模拟

    我觉得这道题的主旨应该是模拟,但是如果说他是二维链表的話也不為過.這道題的主體思路就是把原來旋轉點的O(n^2)變成了旋轉邊界的O(n).怎麼旋轉邊界呢,就好像是把原來的那些點都於上下左右四個點連線, ...

  3. Membership Service Providers (MSP)

    https://blog.csdn.net/baidu_39649815/article/details/76468249 Membership service provider (MSP)是一个提供 ...

  4. 安卓的progress

    https://www.cnblogs.com/wolipengbo/archive/2013/10/23/3383667.html

  5. POJ2559 Largest Rectangle in a Histogram (单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26012 ...

  6. intellij IDEA与springboot项目建立

    概念问题: IntelliJ系中的Project相当于Eclipse系中的workspace.IntelliJ系中的Module相当于Eclipse系中的Project.IntelliJ中一个Proj ...

  7. Java多线程1:Java中sleep,wait,yield,join的区别

    1.sleep()方法 在指定时间内让当前正在执行的线程暂停执行,但不会释放“锁标志”.不推荐使用. sleep()使当前线程进入阻塞状态,在指定时间内不会执行. 2.wait()方法 在其他线程调用 ...

  8. (转)Python中实现带Cookie的Http的Post请求

    转自crifan: http://www.crifan.com/python_http_post_request_with_cookie/ . . . .

  9. MVC 自定义HtmlHelper帮助类型之分页

    方法一: 在项目中增加App_Code文件夹,新增一个MyHtmlper.cshtml视图文件 写入代码: @helper Pagger(int pageIndex, int pageCount) { ...

  10. Linux引导过程

    早期时,启动一台计算机意味着要给计算机喂一条包含引导程序的纸带,或者手工使用前端面板地址/数据/控制开关来加载引导程序.尽管目前的计算机已经装备了很多工具来简化引导过程,但是这一切并没有对整个过程进行 ...