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 个地铁站, ...
随机推荐
- WPF MATERAIL DESIGN TOOKIT
最近有个程序需要用到WPF,本以为WEB可以做到很炫的,没有想到XAML也能做到如此之炫.心动了,那就行动吧. 搜到有各种款式的:METRO风格,RIBBON风格,MATERIAL风格…… 也许是玩W ...
- Android 坑爹问题
A/art: art/runtime/jdwp/jdwp_event.cc:] Check failed: Thread::Current() != GetDebugThread() (Thread: ...
- 安卓6.0之前的系统 判断app是否有录音权限
public static synchronized boolean isVoicePermission() { AudioRecord record = null; try { record = n ...
- mysql> set sql_mode='no_auto_value_on_zero';
mysql> set sql_mode='no_auto_value_on_zero';
- Mysql如何为表字段添加索引???
1.添加PRIMARY KEY(主键索引): ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQUE(唯一索引) : ALTE ...
- python一周速成学习笔记
目录 一:语法元素 1.注释,变量,空格的使用 2.输入函数,输出函数 3.分支语句,循环语句 4.保留字in,同步赋值 5.import与def以及turtle库 6.eval函数与repr函数 二 ...
- ubuntu 16.04 国内源安装docker
1. 通过curl命令安装 检查是否安装curl root@ros-OptiPlex-3050:~# which curlroot@ros-OptiPlex-3050:~# 更新安装 root@ros ...
- Gym 100425A Luggage Distribution (组合数学,二分)
一开始想着球盒模型,数据范围大,递推会GG. 用凑的方法来算方案.往n个小球之间插两个隔板,方案是(n-1)*(n-2)/2,不区分盒子,三个盒子小球数各不相同的方案数被算了6次(做排列), 两个相同 ...
- gcc, g++ - GNU 工程的 C 和 C++ 编译器 (egcs-1.1.2)
总览 (SYNOPSIS) gcc [ option | filename ]... g++ [ option | filename ]... 警告 (WARNING) 本手册页 内容 摘自 GNU ...
- Python 入门基础
第一章 计算机基础 1.1 硬件 CPU:处理和运算 内存:临时存储数据 硬盘:永久存储系统 操作系统:是一个软件(特殊), 调度每个硬件之间的数据传输 1.2 操作系统 Windows:xp/7/8 ...