题目链接: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. codeforces1045B

    CF1045B 自己瞎鸡巴yy了一下,可知若一个数X不能被表示出来,那么X所有的表示方法都在A集合中,如a1,a2,a3······an-1,an-2中若a1+ai不能被表示出来,那么如果a1到ai是 ...

  2. Ubuntu 16.04安装Maven

    此篇为http://www.cnblogs.com/EasonJim/p/7139275.html的分支页. 前提:必须正确安装JDK. 一.通过二进制包(tar.gz)安装 下载: 进入下载列表:h ...

  3. jQuery筛选总结

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. MT【233】染色正方形

    有$n$个正方形排成一行,今用红,白,黑三种颜色给这$n$个正方形染色,每个正方形只能染一种颜色.如果要求染白色的正方形必须是偶数个,问有多少种不同的染色方法. 解答:设有$a_n$种不同的染法,则$ ...

  5. 03 Zabbix常用的术语

    03 Zabbix常用的术语 host(主机): 要监控的网络设备,可由IP或DNS名称指定 host group(主机组):主机的逻辑容器,可以包含主机和模板,但同一个组内的主机和模板不能互相链接: ...

  6. 用DotNetOpenAuth实现基于OAuth 2.0的web api授权 (一)Getting Start

    1. 下载 源码下载 2. build solution,创建虚拟目录: 右健MyContatacts/MyPromo项目,选择Properties,点击左边的Web,点击 Create Virtua ...

  7. [ZJOI2007]矩阵游戏——非常漂亮的二分图转化

    题意: 小 Q 是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏――矩阵游戏.矩阵游戏在一个 N×N 黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作: ...

  8. Windows下修改Tomcat黑窗口标题

    在Tomcat的bin目录下,新建文件setenv.bat. 在文件内输入: set TITLE=MYTomcat-%date% %time%[%cd%] 可以把MYTomcat修改为自己定义的名字. ...

  9. (转)小谈keepalived vip漂移原理与VRRP协议

    背景:之前搭建过keepalived双机热备的集群,但对其中的原理不甚理解,看完就忘了,所有有必要深入的学习下. 简介 什么是keepalived呢?keepalived是实现高可用的一种轻量级的技术 ...

  10. javascript面向对象精要第三章对象整理精要

    什么是对象的数据属性?什么是对象的访问器属性?[put]方法是默认创建数据属性的,访 问器属性不包含值而是定义了一个单属性被读取时调用的函数(getter)和当一个属性被写入时 调用的函数(sette ...