D - Interesting Calculator

公西财
2023-12-01

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 2561 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 112 256100 100 100 1 100 100 100 100 100 100100 100 100 100 100 1 100 100 100 100100 100 10 100 100 100 100 100 100 100
Sample Output
Case 1: 2 2Case 2: 12 3
 
 
#include<cstdio>
#include<queue>
#include<vector>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
struct node{
int va,co,cost;
friend bool operator<(node a,node b)
{
    if(a.cost==b.cost)return a.co>b.co;
    return a.cost>b.cost;
}
};
int a[3][12];int flag[100005];//刚开始写时,用flag来标记已经得到过的值,标记后就不再入队,结果第二组数据错了;上网一查,发现当前的值(x经过系列按
int main()//扭后得到的值)所对应的代价不一定是最小的,这样那些代价可能更小的就被刷下来了,所以出错。所以这里将flag用作标记那些相同值时代价小的,并让其
{
    int x,y;int k=1;//进堆栈
    while(cin>>x>>y)
    {
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<10;j++)cin>>a[i][j];
        }
        node sta;sta.va=x,sta.co=0,sta.cost=0;memset(flag,0x3f,sizeof(flag));
        priority_queue<node>it;
        it.push(sta);flag[x]=1;int cost=-2,press=-2;
        while(!it.empty())
        {
            node now=it.top();it.pop();
            node pre;
            if(now.va==y){cost=now.cost,press=now.co;break;}
            for(int i=0;i<3;i++)
            {
                for(int j=0;j<10;j++)
                {

                    if(i==0)pre.va=now.va*10+j;
                    else if(i==1)pre.va=now.va+j;
                    else pre.va=now.va*j;
                    pre.co=now.co+1;pre.cost=now.cost+a[i][j];
                    if(pre.va<=y&&pre.cost<flag[pre.va]){it.push(pre);flag[pre.va]=pre.cost;}
                }
            }
        }
        if(press>-2)printf("Case %d: %d %d\n",k++,cost,press);
    }

}

 类似资料: