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 ...
随机推荐
- Nginx CONTENT阶段 static模块
L63-65 alias指令 syntax: alias path;# 静态文件路径 alias不会将请求路径后的路径添加到 path中 context : location; root指令 sy ...
- BZOJ4001[TJOI2015]概率论——卡特兰数
题目描述 输入 输入一个正整数N,代表有根树的结点数 输出 输出这棵树期望的叶子节点数.要求误差小于1e-9 样例输入 1 样例输出 1.000000000 提示 1<=N<=10^9 设 ...
- ACG图片站\python爬虫\LAMP环境
最近突然对web很感兴趣,碰巧看到阿里云服务器学生价十块钱一个月,果断买了一个自己搭建了一个网站. 网址 这里 LAMP环境就搭建了好久,linux+apache2+mysql+php,都是开源的软件 ...
- java监控工具VisualVM
java监控工具VisualVM https://visualvm.github.io/ https://visualvm.github.io/documentation.html https://h ...
- 【Gym 100015B】Ball Painting(DP染色)
题 There are 2N white balls on a table in two rows, making a nice 2-by-N rectangle. Jon has a big pai ...
- Centos7安装Zabbix4.0步骤
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 Centos7安装Zabbix4.0步骤 官方搭建zabbix4.0的环境要求: 1. 环境搭建L ...
- 自学Linux Shell16.2-函数中使用变量
点击返回 自学Linux命令行与Shell脚本之路 16.2-函数中使用变量 1. 向函数传递参数 函数可以使用标准参数环境变量来表示命令行传递给函数的参数.例如, 函数名在变量$0中定义,函 ...
- 自学Python1.5-Centos内python2识别中文
自学Python之路 自学Python1.5-Centos内python2识别中文 方法一,python推荐使用utf-8编码方案 经验一:在开头声明: # -*- coding: utf-8 -*- ...
- poj2386(简单的dfs/bfs)
题目链接:http://poj.org/problem?id=2386 Description Due to recent rains, water has pooled in various pla ...
- A1027. Colors in Mars
People in Mars represent the colors in their computers in a similar way as the Earth people. That is ...