CSU 1336 Interesting Calculator(spfa)

本文介绍了一款特殊的计算器,并提出一个有趣的问题:如何利用SPFA算法计算从一个数转换到另一个数所需的最小成本和最少按键次数。通过具体的代码实现和样例输入输出,展示了算法的高效性和实用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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

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



思路:变形的spfa,关键还是在松弛条件处,对于每一次的松弛都有30种情况需要判断……..具体详见代码

代码:

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define maxn 100000+10
const int inf=0x3f3f3f3f;
int d[maxn],inq[maxn],cost[5][15],step[maxn];

void spfa(int st,int ed)
{
    mem(d,inf);
    mem(step,inf);
    mem(inq,0);
    d[st]=step[st]=0,inq[st]=1;
    queue<int>q;
    q.push(st);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        inq[st]=0;
        for(int i=0; i<3; ++i)
            for(int j=0; j<10; ++j)
            {
                int x;
                if(i==0)
                    x=st*10+j;
                else if(i==1)
                    x=st+j;
                else if(i==2)
                    x=st*j;
                if(x>ed)
                    continue;
                if(d[x]>d[st]+cost[i][j])
                {
                    d[x]=d[st]+cost[i][j];
                    step[x]=step[st]+1;
                    if(!inq[x])
                    {
                        inq[x]=1;
                        q.push(x);
                    }
                }
                else if(d[x]==d[st]+cost[i][j]&&step[x]>step[st]+1)
                {
                    step[x]=step[st]+1;
                    if(!inq[x])
                    {
                        inq[x]=1;
                        q.push(x);
                    }
                }
            }
    }
    printf("%d %d\n",d[ed],step[ed]);
}

int main()
{
    int st,ed,T=1;
    while(~scanf("%d%d",&st,&ed))
    {
        for(int i=0; i<3; ++i)
            for(int j=0; j<10; ++j)
                scanf("%d",&cost[i][j]);
        printf("Case %d: ",T++);
        spfa(st,ed);
    }
    return 0;
}



总结:刚开始还以为是DP呢,结果想了半天也没想出来DP怎样写。又看大佬用spfa把它给A了,顿时觉得我的最短路真水。。。(思维真是太局限了)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值