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

题意:为了使 第一个数变成 第二个数  共有30种操作 在前一个数末尾加 0~9  前一个数加上 0~9  前一个数乘 0~9
每种操作都需要一定的费用 以表给出
求变成第二个的最小费用和它的操作次数

这道题考的是对优先队列的掌握。。。 
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node {
  int x,num,sum;
  friend bool operator<(const node aa, node bb)
    {
        if(aa.sum!=bb.sum)
            return aa.sum>bb.sum;  //费用小优先
            else
                return aa.num>bb.num; //费用相同 次数少优先
    }
};
int vis[100005];  //标记这个数是否出现过
int q[4][20];
int a,b;
int ans,s;
void bfs()
{
    memset(vis,0,sizeof(vis));
    int i;
   priority_queue<node>qq;
   node now,next;
   now.num=0;
   now.sum=0;
   now.x=a;
   qq.push(now);
   while(!qq.empty())
   {
       now=qq.top();
       qq.pop();
       if(vis[now.x])   //  此题的关键
        continue;
       vis[now.x]=1;    //  此题的关键    利用优先队列  标记当前这个数 费用最小的状态  过滤那些 费用大于它的同一状态
       if(now.x==b)
       {
           ans=now.sum;
           s=now.num;
           return;
       }
       for(i=0;i<=9;i++)
       {
           next.x=now.x*10+i;
           next.sum=now.sum+q[1][i];
           if(next.x<=b&&!vis[next.x])
           {
                                        //标记状态的VIS[]数组不能出现在这里。。 否则就不是最小费用了。。
           next.num=now.num+1;
           qq.push(next);
            }
           next.x=now.x+i;
           next.sum=now.sum+q[2][i];
           if(next.x<=b&&!vis[next.x])
           {
           next.num=now.num+1;
           qq.push(next);
           }
           next.x=now.x*i;
        next.sum=now.sum+q[3][i];
           if(next.x<=b&&!vis[next.x])
           {

           next.num=now.num+1;
           qq.push(next);
            }
       }
   }
}
int main()
{
    int i,j;
    int t=0;
    while(~scanf("%d%d",&a,&b))
    {
        t++;
        for(i=1;i<=3;i++)
            for(j=0;j<=9;j++)
                scanf("%d",&q[i][j]);
        bfs();
        printf("Case %d: %d %d\n",t,ans,s);
    }

    return 0;
}

时间: 2024-10-22 01:22:33

CSUOJ 1336 Interesting Calculator 优先队列的相关文章

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

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 disp

湖南省第九届大学生计算机程序设计竞赛 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

UVa - 12664 - Interesting Calculator

先上题目: 12664 Interesting CalculatorThere 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. Pressin

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

BFS CSU

因为要花费最少,如果花费最少的有多个还要使得步数最少 所以在判断一个数字要不要入队列的时候只要判断这个就可以了 I - Interesting Calculator Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Practice CSU 1336 Description There is an interesting calculator. It has 3 ro

训练16

3991: Electoral Rolls Revision 题意:对n对数排序,从小到大输出 #include<bits/stdc++.h> using namespace std; inline int read() { int x=0; char c=getchar(); bool flag=0; while(c<'0'||c>'9'){if(c=='-')flag=1; c=getchar();} while(c>='0'&&c<='9'){x=

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

I Interesting Calculator CSU 过了 TOJ超时了 先记一下 #include <cstdio> #include <cstring> #include <queue> using namespace std; int a[100]; bool b[100010]; int c[100010]; int d[100010]; int x, y; int cas = 1; struct node { int x, t, tt; node(){}

hdu5336 多校联合第四场1010 模拟+bfs优先队列

http://acm.hdu.edu.cn/showproblem.php?pid=5336 Problem Description XYZ is playing an interesting game called "drops". It is played on a r?c grid. Each grid cell is either empty, or occupied by a waterdrop. Each waterdrop has a property "siz