I - Interesting Calculator (bfs使用优先队列求步数最小或者花费最小)
题目链接: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使用优先队列求步数最小或者花费最小)的更多相关文章
- bfs和优先队列求数据三角形最大值
此题用深搜很快就解决,我用宽度搜索和优先队列仅仅是为了练习他们的用法:深搜法在注释内: 1 #include<iostream> #include<cstring> #incl ...
- D - Interesting Calculator 【数值型BFS+优先队列】
There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...
- 中南大学oj:1336: Interesting Calculator(广搜经典题目)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 There is an interesting calculator. It has 3 r ...
- CSU-1336: Interesting Calculator,最短路思想!
1336: Interesting Calculator 这道题被LZQ抢了一血,于是去读题发现题意不难,纯广搜结果写的一塌糊涂. 题意:给你两个数x,y.由x每次可以经过一系列操作变换,每个变换都有 ...
- HDU 1254 推箱子(BFS加优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1254 推箱子 Time Limit: 2000/1000 MS (Java/Others) Me ...
- 湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator
Interesting Calculator Time Limit: 2 Sec Memory Limit: 128 MB Submit: 163 Solved: 49 Description T ...
- UVa - 12664 - Interesting Calculator
先上题目: 12664 Interesting CalculatorThere is an interesting calculator. It has 3 rows of button.• Row ...
- 求树的最大独立集,最小点覆盖,最小支配集 贪心and树形dp
目录 求树的最大独立集,最小点覆盖,最小支配集 三个定义 贪心解法 树形DP解法 (有任何问题欢迎留言或私聊&&欢迎交流讨论哦 求树的最大独立集,最小点覆盖,最小支配集 三个定义 最大 ...
- HDU——1242Rescue(BFS+优先队列求点图最短路)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
随机推荐
- 使用TortoiseSVN创建版本库
1. 使用TortoiseSVN创建版本库 在SVN中,为了便于创建分支和标签,我们习惯于将Repository版本库的结构布置为:/branches,/tags,/trunk.分别代表分支,标签以及 ...
- BZOJ2793[Poi2012]Vouchers——枚举
题目描述 考虑正整数集合,现在有n组人依次来取数,假设第i组来了x人,他们每个取的数一定是x的倍数,并且是还剩下的最小的x个.正整数中有m个数被标成了幸运数,问有哪些人取到了幸运数. 输入 第一行一个 ...
- BZOJ3724 PA2014Final Krolestwo(欧拉回路+构造)
如果没有长度为偶数的限制,新建一个点向所有奇点连边,跑欧拉回路即可,显然此时一定存在欧拉回路,因为所有点度数都为偶数. 考虑长度为偶数的限制,将每个点拆成两个点放进一个二分图里,那么每条原图中的边在二 ...
- Codeforces Round #418 (Div. 2) B. An express train to reveries
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- MT【10】和三次有关的一个因式分解
解答: 评:1此处因式分解也可以看成关于$a$的函数$f(a)$利用多项式有理根的有关知识得到 2.此处我们可以得到关于$\Delta ABC$的余弦的一个不等式$cosA+cosB+cosC> ...
- 自学Linux Shell6.3-系统环境变量持久化
点击返回 自学Linux命令行与Shell脚本之路 6.3-系统环境变量持久化 在你登录Linux系统启动一个bash shell时,默认情况下bash在几个文件中查找命令,这几个文件成为启动文件:b ...
- 自学Python6.2-类、模块、包
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- rt-thread learning notes
rt-thread learning notes 2018-01-15 > 001 具有相同优先级的线程,每个线程的时间片大小都可以在初始化或创建该线程时指定 rt_thread_t rt_th ...
- 【转】【JLINK下载失败,STLINK下载失败万能解决方案】JLINK和STLINK都无法下载时的解决办法,此时芯片并没有报废
很多时候都会认为这个板子无法下载了,其实不是的,有解决办法. 原因如下: 由于客户设置的晶振频率和PLL配置错误时,就会出现这种问题,下载一次程序锁住一次板子,搞的人痛不欲生. 还有错误的外设配置,S ...
- 【AGC014E】Blue and Red Tree
Description 给定一棵\(n\)个节点的蓝边树,再给定一棵\(n\)个节点的红边树.请通过若干次操作将蓝树变成红树.操作要求和过程如下: 1.选定一条边全为蓝色的路径: 2.将路径上的一条蓝 ...