Interesting Calculator

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
Case 1: 2 2
Case 2: 12 3
题意:
让你按下按钮,每个按钮都有一定的花费,,第一种按钮是把对应的数字加到值得后面,第二种是把对应的数字加到值上,第三种是把对应的数字乘到值上面。。让你求把x变成y的最小花费,如果花费一样的话,就优先输出按的次数的最小的那个花费。
思路:spfa
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int a[5][15];
int vis[101000];//记录在对列出现的情况,1表示在队列里有过了,0表示在队列里没有
int dis[100100];//记录到达一个值得最小花费
int num[100100];//记录按的次数
#define inf 0x3f3f3f3f
int x,y;
void we()
{
    memset(vis,0,sizeof(vis));
    memset(dis,inf,sizeof(dis));//初始化最大值
    memset(num,inf,sizeof(num));
    queue<int>q;
    dis[x]=0;
    vis[x]=1;
    num[x]=0;
    q.push(x);
    int ni;
    int v;
    while(!q.empty())
    {
        ni=q.front();
        q.pop();
        vis[ni]=0;
        for(int i=0;i<10;i++)//按下第一行的数的情况
        {
            v=ni*10+i;
            if(v>y)continue;
            if(dis[ni]+a[0][i]<dis[v])//松弛成功
            {
                num[v]=num[ni]+1;
                dis[v]=dis[ni]+a[0][i];
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
            if(dis[ni]+a[0][i]==dis[v]&&num[ni]+1<num[v])//如果花费一样,那么就看按的次数
            {
                num[v]=num[ni]+1;
                if(!vis[v])
                { vis[v]=1;
                    q.push(v);

                }
            }
        }
        for(int i=0;i<10;i++)//按下第二行的情况
        {
            int v;
            v=ni+i;
            if(v>y)continue;
            if(dis[ni]+a[1][i]<dis[v])
            {
                num[v]=num[ni]+1;
                dis[v]=dis[ni]+a[1][i];
                if(!vis[v])
                {vis[v]=1;
                    q.push(v);

                }
            }
            if(dis[ni]+a[1][i]==dis[v]&&num[ni]+1<num[v])
            {
                num[v]=num[ni]+1;
                if(!vis[v])
                {vis[v]=1;
                    q.push(v);

                }
            }
        }
        for(int i=0;i<10;i++)//按下第三行的情况
        {
            int v;
            v=ni*i;
            if(v>y)continue;
            if(dis[ni]+a[2][i]<dis[v])
            {
                num[v]=num[ni]+1;
                dis[v]=dis[ni]+a[2][i];
                if(!vis[v])
                {vis[v]=1;
                    q.push(v);

                }
            }
            if(dis[ni]+a[2][i]==dis[v]&&num[ni]+1<num[v])
            {
                num[v]=num[ni]+1;
                if(!vis[v])
                {vis[v]=1;
                    q.push(v);

                }
            }
        }
    }
}
int g=0;
int main()
{
    while(scanf("%d%d",&x,&y)!=-1)
    {
        ++g;
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<10;j++)
            {
                scanf("%d",&a[i][j]);
            }
        }
        we();
        printf("Case %d: %d %d\n",g,dis[y],num[y]);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值