给出一个地图,地图有四种路面,经过每种路面花费的时间不同,问从起点到终点所花费的最少时间是多少

把到各个点的花费存入队列中,然后弹出,即可得到最小

Sample Input
4 6
1 2 10
T...TT
TTT###
TT.@#T
..###@
0 1 3 0

4 6
1 2 2
T...TT
TTT###
TT.@#T
..###@
0 1 3 0

2 2
5 1 3
T@
@.
0 0 1 1

 
Sample Output
Case 1: 14
Case 2: 8
Case 3: -1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int x,y,v1,v2,v3,b,n,m;
int x1,x2,y1,y2;
int p[25][25];
int vis[25][25];
int v[5];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
int x,y,step;
friend bool operator < (node a,node b)
{
return a.step > b.step;
}
}; bool judge(int x,int y)
{
if(x < 0 || y < 0 || x >= n || y >= m || p[x][y] < 0 || vis[x][y])
return false;
return true;
} int work()
{
priority_queue<node>que;
node a,b;
a.x=x1;
a.y=y1;
a.step = 0;
que.push(a);
memset(vis,0,sizeof(vis));
vis[x1][y1] = 1;
while(!que.empty())
{
a = que.top();
que.pop();
if(a.x == x2 && a.y == y2)
return a.step;
for(int i = 0;i < 4;i++)
{
b = a;
b.x += dir[i][0];
b.y += dir[i][1];
if(!judge(b.x,b.y))
continue;
b.step += v[p[b.x][b.y]];
vis[b.x][b.y] = 1;
que.push(b);
}
}
return -1;
} int main()
{
int i,j,cas = 1;
char s[1000];
while(~scanf("%d%d",&n,&m))
{
scanf("%d%d%d",&v[3],&v[2],&v[1]);
for(i = 0; i < n; i++)
{
scanf("%s",s);
for(j = 0;s[j]; j++)
{
if(s[j]=='T') p[i][j] = 1;
else if(s[j] == '.') p[i][j] = 2;
else if(s[j] == '#') p[i][j] = 3;
else if(s[j] == '@') p[i][j] = -1;
}
}
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans = work();
printf("Case %d: %d\n",cas++,ans);
}
return 0;
}

  

HDU2425:Hiking Trip(BFS+优先队列)的更多相关文章

  1. hdu 2425 Hiking Trip (bfs+优先队列)

    Problem Description Hiking in the mountains is seldom an easy task for most people, as it is extreme ...

  2. hdu 2425 Hiking Trip

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...

  3. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  4. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  5. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  6. hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)

    题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...

  7. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  8. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  9. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

随机推荐

  1. iOS开发-OC中TabView的编辑

    UITableView编辑 1> UITableView 编辑流程 2> UITableView 编辑步骤(四步) ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事件方法 ...

  2. H5 FormData对象的作用及用法

    JS: function uploadFileAndParam() { var url = "http://localhost:42561/api/upload/UploadPost&quo ...

  3. 3-51单片机WIFI学习(开发板8266底层源码介绍)

    上一篇链接  http://www.cnblogs.com/yangfengwu/p/8743502.html 直接上源码:注意源码有两部分,第一部分是一开始的时候写在模块内部的,另一部分是存在手机内 ...

  4. Mego开发文档 - 基本保存操作

    基本保存操作 在Mego中没有更改跟踪,也就是说所有的新增.更新及删除都需要开发者自行判断.Mego会最为实际的将各个数据操作提交给数据库并执行. 添加数据 using (var db = new O ...

  5. SpringBoot单元测试中的事务和Session

    1.Springboot中使用junit编写单元测试,并且测试结果不影响数据库. 2.

  6. redis入门(04)redis的数据类型

    Redis 数据类型 Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合). 1.String(字符串) ...

  7. nodejs(1-1)

    http://www.runoob.com/nodejs/nodejs-tutorial.html

  8. RxJava系列6(从微观角度解读RxJava源码)

    RxJava系列1(简介) RxJava系列2(基本概念及使用介绍) RxJava系列3(转换操作符) RxJava系列4(过滤操作符) RxJava系列5(组合操作符) RxJava系列6(从微观角 ...

  9. oracle获取表字段属性

    select b.COMMENTS,a.COLUMN_NAME,a.DATA_TYPE,a.DATA_LENGTH, a.DATA_PRECISION,a.DATA_SCALE,a.NULLABLE, ...

  10. 关于CheckStyle在eclipse出现的问题

    今天在公司换了一个CheckStyle xml文件.那么我尝试直接import进去新的文件. 在我Check code的时候就爆了下面的错误 o: Failed during checkstyle c ...