Interesting Calculator UVA
来源:互联网 发布:scp 指定端口 编辑:程序博客网 时间:2024/06/05 14:31
bfs的暴力搜索的优化,bfs的时候如果找到了一个最大值,那么之后bfs的时候如果得到一状态是小于这个最大值的才能继续判断,大于的就不行
然而只是这一种地方的优化是不够的,我们需要建立一个花费数组,mcost,存放着当前状态下的最小花费,如果这个花费比之前的小才可能进栈,
否则就不进入,进栈的时候同事更新他的最小值。
#include<bits/stdc++.h>using namespace std;int cost[4][10];int mcost[100861];struct aa{ int step; int cost; int num;} t,tt;int kase=1;void bfs(int s,int e){ int minstep=0x3f3f3f3f; int mincost=0x3f3f3f3f; t.num=s; t.step=0; t.cost=0; queue<struct aa>ycq; ycq.push(t); mcost[s]=0; while(!ycq.empty()) { t=ycq.front(); ycq.pop(); if(t.num==e) { if(t.cost<mincost) { mincost=t.cost; minstep=t.step; } continue; } for(int i=0; i<10; i++)//操作3 { tt.num=t.num*i; tt.cost=t.cost+cost[3][i]; tt.step=t.step+1; if(tt.num>e||tt.cost>mincost) continue; if(tt.cost<mcost[tt.num]) { ycq.push(tt); mcost[tt.num]=tt.cost; } } for(int i=0; i<10; i++)//操作1 { tt.num=t.num*10+i; tt.cost=t.cost+cost[1][i]; tt.step=t.step+1; if(tt.num>e||tt.cost>mincost) continue; if(tt.cost<mcost[tt.num]) { ycq.push(tt); mcost[tt.num]=tt.cost; } } for(int i=0; i<10; i++)//操作2 { tt.num=t.num+i; tt.cost=t.cost+cost[2][i]; tt.step=t.step+1; if(tt.num>e||tt.cost>mincost) continue; if(tt.cost<mcost[tt.num]) { ycq.push(tt); mcost[tt.num]=tt.cost; } } } printf("Case %d: %d %d\n",kase++,mincost,minstep);}int main(){ int s,e; while(scanf("%d%d",&s,&e)!=EOF) { for(int i=0; i<=e; i++) mcost[i]=0x3f3f3f3f; for(int i=1; i<=3; i++) { for(int j=0; j<10; j++) scanf("%d",&cost[i][j]); } bfs(s,e); }}
阅读全文
0 0
- UVA 12664 Interesting Calculator
- Interesting Calculator UVA
- Interesting Calculator UVA
- Interesting Calculator UVA
- UVA 12664 Interesting Calculator(spfa)
- Interesting Calculator
- Interesting Calculator
- Uva 12664 Interesting Calculator 最短路 递推 优化技巧
- Interesting Calculator CSU
- TOJ 4603 Interesting Calculator -- SPFA
- Interesting Calculator 湖南第九届省赛
- CSU-1336--Interesting Calculator(bfs)
- CSU~1336: Interesting Calculator(spfa)
- CSU1336(湖南第九届原题) Interesting Calculator
- CSUOJ 1336 Interesting Calculator spfa / 优先队列
- CSUOJ 1336 Interesting Calculator (spfa算法)
- CSUOJ 1336 Interesting Calculator(BFS+优先队列)
- CSU 1336 Interesting Calculator(spfa)
- 微信一次性订阅消息
- react-router HashRouter和BrowserRouter的使用
- Codeforces Round #412 (Div. 2)
- ios 图片由小到大和由大到小的渐进效果
- linux下安装squid
- Interesting Calculator UVA
- vim使用手册
- Qt的XML文件处理
- mac 终端连接到服务器
- Guava Table<R,C,V>
- java8之lambda表达式
- 通过缓冲流把一个文本文件中的字符串排序后输入到另一个文件中
- hdu 6047 Maximum Sequence
- Python数据分析常用函数笔记