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 个地铁站, ...
随机推荐
- Java开发笔记(九十六)线程的基本用法
每启动一个程序,操作系统的内存中通常会驻留该程序的一个进程,进程包含了程序的完整代码逻辑.一旦程序退出,进程也就随之结束:反之,一旦强行结束进程,程序也会跟着退出.普通的程序代码是从上往下执行的,遇到 ...
- vs2013编译过程中,错误 59 error C4996: 'GetVersionExW': 被声明为已否决
好几次碰到这个错误,必须mark 一下!!!!!Project Properties > Configuration Properties > C/C++ > General > ...
- List 集合中数据不重复的使用
foreach (DataRow dr in dt.Rows) { list.Add(dr["项目组"].ToString()); } list = list.Distinct&l ...
- thinkphp写的登录注册的小demo
和asp.net类似,一个FormAction对应Form文件夹 demo结构: ‘ 对于项目结构有疑问的: http://www.thinkphp.cn/document/60.html login ...
- ios UnitTest 学习笔记1
一.运行第一个单元测试: 1.在Xcode 5中新建一个工程默认自带一个单元测试的文件夹,IDE自动生成了一个实现XCTestCase的.m文件,里面有一个失败测试(早期版本中实现的是SenTestC ...
- ajax报错问题的解决
背景:用ajax与服务器页面进行交互 问题:XMLHttpRequest.status==0并且XMLHttpRequest.readyState==0并且textStatus==error 关于XM ...
- JavaScript(appendChild添加节点)
首先,我们有一个编辑器,有一个简单的HTML页面,页面的级别分别 --> html -->head[title,meta,script,link] -- body,然后再新建一个inde ...
- 你是猴子请来的逗比么!IT跳槽大事件
3月招聘大战早已硝烟四起,互联网职场摇身一变成了跳蚤市场,猎头们告诉跳蚤们,跳不跳不是不问题,往哪儿跳才是重点,跳对了高薪期权都如过眼云烟.不过小编不得不说,劳资最痛恨那些跳槽的人啦!就因为加班 ...
- ansible 调优
1.设置ssh长链接ssh_args = -C -o ControlMaster=auto -o ControlPersist=5d 2.开启pipelining开启pipelining 需要被控制机 ...
- [Android 测试] 压力稳定性测试之: Monkey 详解分析脚本(转载)
一.什么是稳定性测试? 通过随机点击屏幕一段时间,看看app会不会奔溃,能不能维持正常运行. 二. Money是什么? Monkey测试是Android平台自动化测试的一种手段,通过Monkey程序模 ...