The Ninth Hunan Collegiate Programming Contest (2013) Problem I

计算器最小成本转换
本文介绍了一个有趣的问题:如何使用一种特殊的计算器将显示从一个数x转换到另一个数y,同时花费的成本最少。此计算器拥有三排按钮,分别用于追加数字、加法操作和乘法操作。文章详细讨论了通过广度优先搜索算法来解决该问题的方法。

Problem I

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

Output for the Sample Input

Case 1: 2 2
Case 2: 12 3

The Ninth Hunan Collegiate Programming Contest (2013)
Problemsetter: Rujia Liu
Special Thanks: Feng Chen, Md. Mahbubul Hasan

 

  典型的广搜,有最优性,用堆来优化可以加速运行,这种试题本质很老,需要认识本质,找准突破口。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const LL inf=(LL)10000000000000 ;
int num[4][10] ;
LL dist[100008] ;
LL step[100008] ;
int x, y;
struct  Node{
     int Num ;
     LL money ;
     LL Step ;
     Node(){} ;
     Node(int n ,LL m ,LL s):Num(n),money(m),Step(s){} ;
     friend bool operator <(const Node A ,const Node B){
          if(A.money==B.money)
                return A.Step>B.Step ;
          else
                return A.money>B.money ;
     }
};
void bfs(){
    priority_queue<Node>que ;
    fill(step,step+y+1,inf) ;
    fill(dist,dist+y+1,inf) ;
    que.push(Node(x,0,0)) ;
    dist[x]=0 ;
    step[x]=0 ;
    while(!que.empty()){
        Node now=que.top() ;
        que.pop() ;
        if(now.Num==y){
             return ;
        }
        int next_id ;
        for(int k=1;k<=3;k++){

            for(int i=0;i<=9;i++){
                if(k==1)
                   next_id=now.Num*10+i ;
                else if(k==2)
                   next_id=now.Num+i ;
                else if(k==3)
                   next_id=now.Num*i  ;
                if(next_id>y)
                    continue  ;
                if(now.money+num[k][i]<dist[next_id]){
                       dist[next_id]=now.money+num[k][i] ;
                       step[next_id]=now.Step+1 ;
                       que.push(Node(next_id,dist[next_id],step[next_id])) ;
                }
                else if(now.money+num[k][i]==dist[next_id]){
                       if(now.Step+1<step[next_id]){
                             step[next_id]=now.Step+1 ;
                             que.push(Node(next_id,dist[next_id],step[next_id])) ;
                       }
                }
            }

        }

    }
}
int main(){
    int k=1 ;
    while(scanf("%d%d",&x,&y)!=EOF){
         for(int i=1;i<=3;i++)
            for(int j=0;j<=9;j++)
                scanf("%d",&num[i][j]) ;
         bfs() ;
         printf("Case %d: ",k++) ;
         cout<<dist[y]<<" "<<step[y]<<endl ;
    }
    return 0 ;
}

 

转载于:https://www.cnblogs.com/liyangtianmen/p/3371178.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值