D - Interesting Calculator 【BFS+优先队列】

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[100005];
int mp[5][15];
int cas=1;
int x,y,i,j;

void bfs()
{
    node.time=0;
    node.cost=0;
    node.value=x;
    while(!q.empty()) q.pop();
    q.push(node);
    val[x]=0;
    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=0;i<3;i++)
        {
            for(int j=0;j<=9;j++)
            {
                if(i==0)
                    tp.value=tmp.value*10+j;
                else if(i==1)
                    tp.value=tmp.value+j;
                else
                    tp.value=tmp.value*j;

                    tp.cost=tmp.cost+mp[i][j];
                    tp.time=tmp.time+1;

               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=0;i<100005;i++)
        val[i]=999999;
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<10;j++)
            {
                scanf("%d",&mp[i][j]);
            }
        }
        bfs();
    }
    return 0;
}

				
时间: 2024-10-08 15:37:03

D - Interesting Calculator 【BFS+优先队列】的相关文章

CSUOJ 1336 Interesting Calculator 优先队列

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

CSU 1336: Interesting Calculator(BFS啊 湖南省第九届大学生计算机程序设计竞赛)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 1336: Interesting Calculator 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

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

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 t

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 dis

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

hdu 2102 A计划 详细题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 开始看到四分之一的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,不过在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间.果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,只不过多加了个参数,就是迷宫的层数,我用0代表第一层,1代表第二层,这在数组里面会体现的. struct node { int index;//层数