CSU-1336: Interesting Calculator,最短路思想!
1336: Interesting Calculator
这道题被LZQ抢了一血,于是去读题发现题意不难,纯广搜结果写的一塌糊涂。
题意:给你两个数x,y。由x每次可以经过一系列操作变换,每个变换都有一定的费用,求x变换到y的最小费用下最小步数。
思路:类似最短路的思想,当时只想到每个点有30个方向可以走,于是直接广搜,但不是TLE就是MLE,后来比完赛看题解才知道要用优先队列。最小费用优先,然后最小步数优先。每一步都是前一步到达的,但一个点可以由多个点到达,这时我们肯定要取最优的那一步嘛。如果我们不用标记,那么就存在重复走的过程,这样TLE和MLE是很合理的,但标记的话如果不用优先队列就可能导致这一步并不是由最优路线得到的,而我们直接就将其堵塞了。。。这个题关键就是这么最短路的思想,而且我们保证第一次到达这个y点一定是最优解,直接return,否则还是会超时的。
const int N=1e5+10;
int x,y,ans1,ans2;
int a[4][15],v[N];
struct node
{
int step,num,cost;
friend bool operator <(const node x,const node y)
{
return x.cost>y.cost||(x.cost==y.cost&&x.step>y.step);
}
} D;
void bfs()
{
memset(v,0,sizeof(v));
priority_queue<node>q;
D.num=x,D.cost=0,D.step=0;
q.push(D);
while(!q.empty())
{
node tmp=q.top();
q.pop();
int xx=tmp.num,step=tmp.step,cost=tmp.cost;
if(v[xx]) continue;
v[xx]=1;
if(xx==y)
{
if(ans2>cost) ans2=cost,ans1=step;
else if(ans2==cost) ans1=min(ans1,step);
return ;
}
for(int i=1; i<4; i++)
for(int j=0; j<10; j++)
{
int x1=xx;
if(i==1) x1=x1*10+j;
else if(i==2) x1+=j;
else x1*=j;
if(x1<=y&&!v[x1]&&(step+1)<=ans1&&cost+a[i][j]<=ans2)
{
D.num=x1,D.cost=cost+a[i][j],D.step=step+1;
q.push(D);
}
}
}
}
int main()
{
int t=1;
while(~scanf("%d%d",&x,&y))
{
memset(a,0,sizeof(a));
for(int i=1; i<4; i++)
for(int j=0; j<10; j++) scanf("%d",&a[i][j]);
ans1=INF,ans2=INF;
bfs();
printf("Case %d: %d %d\n",t++,ans2,ans1);
}
return 0;
} /**********************************************************************
Problem: 1336
User: liuyuqiang
Language: C++
Result: AC
Time:1012 ms
Memory:26672 kb
**********************************************************************/
CSU-1336: Interesting Calculator,最短路思想!的更多相关文章
- 中南大学oj:1336: Interesting Calculator(广搜经典题目)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 There is an interesting calculator. It has 3 r ...
- [Usaco2007 Jan]Telephone Lines架设电话线[二分答案+最短路思想]
Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...
- D - Interesting Calculator 【数值型BFS+优先队列】
There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...
- 湖南省第九届大学生计算机程序设计竞赛 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 ...
- csu - 1659 Graph Center(最短路)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1659 题意是找一个图的中心,图的中心定义是某一个点到其他点的最大距离最小,如果有多个排序输出. 注 ...
- CSU 1259 bfs找最短路
题目大意: 不想介绍,题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1259 bfs求最短路. 这里因为2-9,到达同样的点不计步数,那我 ...
- CF 672C 两个人捡瓶子 最短路与次短路思想
C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CSU 1808 地铁(最短路变形)
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意: Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站, ...
随机推荐
- siege4压测脚本示例
agent="Siege 1.0"rcconfig="/opt/siege4.0/etc/siegerc"concurrent=$1repet=$2url=&q ...
- 按Home键切换到后台后会触发libGPUSupportMercury.dylib: gpus_ReturnNotPermittedKillClient导致crash
转自:http://www.eoeandroid.com/thread-251598-1-1.html 好像有很多朋友都碰到过这个问题,即在真机调试时,按hone键返回桌面,再回到app时,app会c ...
- 100行代码让您学会JavaScript原生的Proxy设计模式
面向对象设计里的设计模式之Proxy(代理)模式,相信很多朋友已经很熟悉了.比如我之前写过代理模式在Java中实现的两篇文章: Java代理设计模式(Proxy)的四种具体实现:静态代理和动态代理 J ...
- iOS打包上传app store各种问题解决总结
问题1 this action could not be completed. try again 问题2 there was an error sending data to the iTunes ...
- Codeforces Gym 100650B Countdown (离线)
题目链接:http://codeforces.com/gym/100650 根据给出的树和d,求出一些结点,这些结点形成子树的第d层结点数应该尽量多,具体要求可以参考题目. dfs一个结点前保存询问深 ...
- 爆零系列—补题A
http://codeforces.com/contest/615/problem/A 读错题 结果发现是无脑题 直接标记统计 #include<cstdio> #include< ...
- MINST手写数字识别(一)—— 全连接网络
这是一个简单快速入门教程——用Keras搭建神经网络实现手写数字识别,它大部分基于Keras的源代码示例 minst_mlp.py. 1.安装依赖库 首先,你需要安装最近版本的Python,再加上一些 ...
- jsc 解码窥探
先使用 JS_DecodeScript反编译jsc 得到AST树 AST树词法解析 http://esprima.org/ AST还原成源码: npm install escodegen AST树遍 ...
- Python字符编码补充
字符编码: Python字符编码贯穿Python学习的始终,现在应用的是Python2中字符编码的问题是很多的. 这次是要彻底解决Python字符编码的问题!!! 1 字符编码的发展过程: 1 .AS ...
- HTTP协议详解-基础知识
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.绝大多数的Web开发,都是构建在HTTP协议之上的Web应用. HTTP协议的主要特点可概括如下: 简单: ...