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 个地铁站, ...
随机推荐
- Sunday算法模板
Sunday是一个线性字符串模式匹配算法.算法的概念如下: Sunday算法是Daniel M.Sunday于1990年提出的一种字符串模式匹配算法.其核心思想是:在匹配过程中,模式串并不被要求一定要 ...
- ios 自定义加载动画效果
在开发过程中,可能会遇到各种不同的场景需要等待加载成功后才能显示数据.以下是自定义的一个动画加载view效果. 在UIViewController的中加载等到效果,如下 - (void)vi ...
- spark性能测试理论-Benchmark(转)
一.Benchmark简介Benchmark是一个评价方式,在整个计算机领域有着长期的应用.正如维基百科上的解释“As computer architecture advanced, it becam ...
- mysqldumpslow及explain使用简介
- SQL 基本编程
定义变量 赋值 取值 分支语句 循环语句 定义变量 declare @变量 数据类型 //@必须带着 不然程序不知道变量是什么 不带@ 电脑会报错 例如 declare ...
- SnowKiting 2017/1/24
原文 Let's go fly a kite...in the snow Your snowkiting checklist To snowkite safely,you'll need a litt ...
- UVA 12549 Sentry Robots (最小点覆盖)
这道题挺像hdu 5093 Battle ships的,不过那道题是要求最多放置的点数,而这道题是要求最小点覆盖. 顶点覆盖的定义是:在G中任意边至少有一个端点属于顶点集合S. 一个重要的位置有(x, ...
- 一张图看懂苹果MacBook所有屏幕分辨率
苹果全新12寸超薄MacBook比曾经最薄的MacBook Air更薄,不过却配备了Retina视网膜显示屏.12英寸RetinaMacBook上的显示屏分辨率为2304*1440,虽然不如15寸和1 ...
- Kernel Stack Overflow(转)
0x00 漏洞代码 stack_smashing.c #include <linux/init.h> #include <linux/module.h> #include &l ...
- java在线聊天项目 使用SWT快速制作登录窗口,可视化窗口Design 更换窗口默认皮肤(切换Swing自带的几种皮肤如矩形带圆角)
SWT成功激活后 new一个JDialog 调整到Design视图 默认的视图模式是BorderLayout,无论你怎么拖拽,只能放到东西南北中的位置上 所以,我们把视图模式调整为AbsoluteLa ...