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,最短路思想!的更多相关文章

  1. 中南大学oj:1336: Interesting Calculator(广搜经典题目)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 There is an interesting calculator. It has 3 r ...

  2. [Usaco2007 Jan]Telephone Lines架设电话线[二分答案+最短路思想]

    Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N ...

  3. D - Interesting Calculator 【数值型BFS+优先队列】

    There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...

  4. 湖南省第九届大学生计算机程序设计竞赛 Interesting Calculator

    Interesting Calculator Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 163  Solved: 49 Description T ...

  5. UVa - 12664 - Interesting Calculator

    先上题目: 12664 Interesting CalculatorThere is an interesting calculator. It has 3 rows of button.• Row ...

  6. csu - 1659 Graph Center(最短路)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1659 题意是找一个图的中心,图的中心定义是某一个点到其他点的最大距离最小,如果有多个排序输出. 注 ...

  7. CSU 1259 bfs找最短路

    题目大意: 不想介绍,题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1259 bfs求最短路. 这里因为2-9,到达同样的点不计步数,那我 ...

  8. CF 672C 两个人捡瓶子 最短路与次短路思想

    C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. CSU 1808 地铁(最短路变形)

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意: Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站, ...

随机推荐

  1. 解决常见SVN冲突问题(转)

    转自:http://www.w3cfuns.com/blog-5443287-5403523.html 一个大项目在开发中可能会拆分成几个小项目,分别分去,同时共通的部分再由人做,做完后再统一合并.同 ...

  2. (十一)mybatis之映射器(select)

    映射器 映射器的主要元素有八种: 元素名称 描述 select 查询语句,可自定义参数 insert 插入语句,执行后返回插入的条数 update 更新语句,执行后返回更新的条数 delete 删除语 ...

  3. Spring IOC的Bean对象

    ---恢复内容开始--- 在Spring IOC模块中Bean是非常重要的.在这里我想给大家讲讲关于Bean对象实例化的三种注入方式: 首先,我先讲一下关于Bean对象属性值的两种注入方式:set注入 ...

  4. eclipse报错MA

    今天有个总监晒了张tomcat7.0崩溃忘记怎么搞了,大家都在吐槽"一个[总监](经常水群)竟然不会这个".敲上来. Problem Occurred系列: 'Starting T ...

  5. The - Modcrab——使用贪心策略

    一.题目信息 The - Modcrab 简单翻译一下:Vova有生命值h1,每次攻击值为a1,每瓶药水恢复生命值c1;Modcrab有生命值h2,每次攻击值为a2.在每个关卡开始,Vova有两种选择 ...

  6. Sql Server 自动备份

    1)启动代理服务 服务->Sql server 代理启动 2)设置维护计划 维护计划->设置维护计划向导->修改名称及说明 3)更改计划 4)选择维护任务 5)顺序调整不做解释 6) ...

  7. sscanf函数详解

    #if 0 ,sscanf():从一个字符串中读进与指定格式相符的数据. ,sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源. ,关于正则表 ...

  8. ios copy assign retain

    一,retain, copy, assign区别 1. 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a ...

  9. verilog behaviral modeling -- procedural timing contronls

    1.delay control : an expression specifies the time duration between initially encountering the state ...

  10. linux终端颜色控制

    引言: 由于在c代码中看到过打印彩色字, 又对PS1 想进一步了解,才有了这篇博文.----------------------------------------Linux 终端控制台字体颜色  - ...