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 ...
随机推荐
- jQuery筛选总结
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Leetcode 326.3的幂 By Python
给定一个整数,写一个函数来判断它是否是 3 的幂次方. 示例 1: 输入: 27 输出: true 示例 2: 输入: 0 输出: false 示例 3: 输入: 9 输出: true 示例 4: 输 ...
- 51Nod 1048 1383 整数分解为2的幂
任何正整数都能分解成2的幂,给定整数N,求N的此类划分方法的数量! 比如N = 7时,共有6种划分方法. 7=1+1+1+1+1+1+1 =1+1+1+1+1+2 =1+1+1+2+2 ...
- 【BZOJ3129】[SDOI2013]方程(容斥,拓展卢卡斯定理)
[BZOJ3129][SDOI2013]方程(容斥,拓展卢卡斯定理) 题面 BZOJ 洛谷 题解 因为答案是正整数,所先给每个位置都放一个就行了,然后\(A\)都要减一. 大于的限制和没有的区别不大, ...
- 洛谷 P2466 Sue的小球 解题报告
P2466 [SDOI2008]Sue的小球 题目描述 Sue和Sandy最近迷上了一个电脑游戏,这个游戏的故事发在美丽神秘并且充满刺激的大海上,Sue有一支轻便小巧的小船.然而,Sue的目标并不是当 ...
- 面试 -- Http协议相关(转载)
http请求由三部分组成,分别是:请求行.消息报头.请求正文 HTTP(超文本传输协议)是一个基于请求与响应模式的.无状态的.应用层的协议,常基于TCP的连接方式,HTTP1.1版本中给出一种持续连接 ...
- 树莓派上使用DHCPig进行DHCP池耗尽攻击
安装DHCPig 这个工具依赖Python的Scapy包,如果未安装需要使用pip工具安装. wget https://github.com/kamorin/DHCPig/raw/master/pig ...
- Eclipse:构造函数不提示才发现
用Eclipse快一年了,今天才发现,原来按下 Alt+? 就可以显示构造函数中的参数. 想一想这一年都不知道是怎么过的,遇到构造函数时,郁闷啊... 2007-11-01
- python并发编程之IO模型 (四十九)
IO模型介绍 http://www.cnblogs.com/linhaifeng/articles/7454717.html
- json模块和pickle模块(二十二)
之前我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型, json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用 ...