题目链接:https://cn.vjudge.net/contest/245287#problem/I

代码:

使用普通的队列和优先队列相比,优先队列能更快地找到目的变量。

#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
# define inf 0x3f3f3f3f
const int maxn=1e8+10;
int n,m;
int a[104][105];
int cost[maxn];
struct node
{
    int cost;
    int step;
    int mon;
    node() {}
    node(int xx,int yy,int zz)
    {
        cost=xx;
        step=yy;
        mon=zz;
    }
    friend bool operator < (node a,node b)
    {
        if(a.cost>b.cost)return true;
        else if(a.cost==b.cost&&a.step>b.step)return true;
        return false;
    }
} temp1,temp;
void bfs()
{
    memset(cost,inf,sizeof(cost));
    priority_queue<node>q;
    cost[n]=0;
    q.push(node(0,0,n));
    while(!q.empty())
    {
        temp=q.top();
        q.pop();
        if(temp.mon==m)
        {
            cout<<temp.cost<<" "<<temp.step<<endl;
            return ;
        }
        for(int i=1; i<=3; i++)
        {
            for(int j=1; j<=10; j++)
            {
                if(i==1)
                {
                    temp1.cost=temp.cost+a[i][j];
                    temp1.mon=temp.mon*10+j-1;
                    temp1.step=temp.step+1;
                }
                if(i==2)
                {
                    temp1.cost=temp.cost+a[i][j];
                    temp1.mon=temp.mon+j-1;
                    temp1.step=temp.step+1;
                }
                if(i==3)
                {
                    temp1.cost=temp.cost+a[i][j];
                    temp1.mon=temp.mon*(j-1);
                    temp1.step=temp.step+1;
                }
                if(temp1.mon<=m&&cost[temp1.mon]>temp1.cost)
                {
                    cost[temp1.mon]=temp1.cost;
                    q.push(temp1);
                }
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int s=1;
    while(cin>>n>>m)
    {
        for(int i=1; i<=3; i++)
        {
            for(int j=1; j<=10; j++)
            {
                cin>>a[i][j];
            }
        }
        cout<<"Case "<<s++<<": ";
        bfs();
    }
    return 0;
}

I - Interesting Calculator (bfs使用优先队列求步数最小或者花费最小)的更多相关文章

  1. bfs和优先队列求数据三角形最大值

    此题用深搜很快就解决,我用宽度搜索和优先队列仅仅是为了练习他们的用法:深搜法在注释内: 1 #include<iostream> #include<cstring> #incl ...

  2. D - Interesting Calculator 【数值型BFS+优先队列】

    There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...

  3. 中南大学oj:1336: Interesting Calculator(广搜经典题目)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 There is an interesting calculator. It has 3 r ...

  4. CSU-1336: Interesting Calculator,最短路思想!

    1336: Interesting Calculator 这道题被LZQ抢了一血,于是去读题发现题意不难,纯广搜结果写的一塌糊涂. 题意:给你两个数x,y.由x每次可以经过一系列操作变换,每个变换都有 ...

  5. HDU 1254 推箱子(BFS加优先队列)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  6. 湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator

    Interesting Calculator Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 163  Solved: 49 Description T ...

  7. UVa - 12664 - Interesting Calculator

    先上题目: 12664 Interesting CalculatorThere is an interesting calculator. It has 3 rows of button.• Row ...

  8. 求树的最大独立集,最小点覆盖,最小支配集 贪心and树形dp

    目录 求树的最大独立集,最小点覆盖,最小支配集 三个定义 贪心解法 树形DP解法 (有任何问题欢迎留言或私聊&&欢迎交流讨论哦 求树的最大独立集,最小点覆盖,最小支配集 三个定义 最大 ...

  9. HDU——1242Rescue(BFS+优先队列求点图最短路)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. 睡前小dp-poj3254-状压dp入门

    http://poj.org/problem?id=3254 从这里学的 http://blog.csdn.net/accry/article/details/6607703 状压dp的入门题.一片N ...

  2. P2157 [SDOI2009]学校食堂

    题目描述 小F 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜肴.当然,不同的人口味也不一定相同,但每个人的口味都可以用一个非负整数 ...

  3. 洛谷 P4408 逃学的小孩 解题报告

    P4408 [NOI2003]逃学的小孩 题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:"喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?&q ...

  4. php关于Session和cookie总结

    什么是 Cookie? cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PHP,能够创建并取回 c ...

  5. Tyvj 1518 CPU监控——极恶线段树

    题目大意: 给定一个区间及其各个元素的初值,要求支持如下操作: 1.区间加 2.区间赋值 3.查询区间最大值 4.查询区间历史最大值 分析: 容易想到线段树,但是细思恶极(仔细想想恶心到了极点)的是, ...

  6. 拖拽功能by javascript 和 react 两种实现方法

    使用鼠标移动图片或者移动图像怪有意思的,那这个移动的效果是怎么实现的呢? 在拖动的过程中,我们会涉及到鼠标向下按,以及移动图形,还有我们松开这几个步骤. 当我们将鼠标向下按的时候,我们鼠标点的这个动作 ...

  7. MySQL中的编码问题

    1.查看MySQL数据库的默认编码 (1).使用status命令 mysql> status -------------- mysql Ver 14.14 Distrib 5.5.28, for ...

  8. kafka集群监控之kafka-manager部署(kafka-manager的进程为:ProdServerStart)

    kafka集群监控之kafka-manager部署(ProdServerStart) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 雅虎官网GitHub项目:https://git ...

  9. mySQL数值类型的取值范围

    如下图,int最大为2145483647,手机号码应该用bigint

  10. MySQL日期时间格式化参数

    MySQL中常常会用到对日期的格式化,比如按某时间格式计算间隔,按某时间格式统计信息等等,所以整理了一下日期格式化的参数,可以根据自己的需求进行组合使用.使用例子如下: (1)SELECT DATE_ ...