hdu 2425 Hiking Trip

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2425

Hiking Trip

Description

Hiking in the mountains is seldom an easy task for most people, as it is extremely easy to get lost during the trip. Recently Green has decided to go on a hiking trip. Unfortunately, half way through the trip, he gets extremely tired and so needs to find the path that will bring him to the destination with the least amount of time. Can you help him?
You‘ve obtained the area Green‘s in as an R * C map. Each grid in the map can be one of the four types: tree, sand, path, and stone. All grids not containing stone are passable, and each time, when Green enters a grid of type X (where X can be tree, sand or path), he will spend time T(X). Furthermore, each time Green can only move up, down, left, or right, provided that the adjacent grid in that direction exists.
Given Green‘s current position and his destination, please determine the best path for him.

Input

There are multiple test cases in the input file. Each test case starts with two integers R, C (2 <= R <= 20, 2 <= C <= 20), the number of rows / columns describing the area. The next line contains three integers, VP, VS, VT (1 <= VP <= 100, 1 <= VS <= 100, 1 <= VT <= 100), denoting the amount of time it requires to walk through the three types of area (path, sand, or tree). The following R lines describe the area. Each of the R lines contains exactly C characters, each character being one of the following: ‘T’, ‘.’, ‘#’, ‘@’, corresponding to grids of type tree, sand, path and stone. The final line contains four integers, SR, SC, TR, TC, (0 <= SR < R, 0 <= SC < C, 0 <= TR < R, 0 <= TC < C), representing your current position and your destination. It is guaranteed that Green‘s current position is reachable – that is to say, it won‘t be a ‘@‘ square.
There is a blank line after each test case. Input ends with End-of-File.

Output

For each test case, output one integer on one separate line, representing the minimum amount of time needed to complete the trip. If there is no way for Green to reach the destination, output -1 instead.

Sample Input

4 6
1 2 10
T...TT
TTT###
[email protected]#T
..###@
0 1 3 0

4 6
1 2 2
T...TT
TTT###
[email protected]#T
..###@
0 1 3 0

2 2
5 1 3
[email protected]
@.
0 0 1 1

Sample Output

Case 1: 14
Case 2: 8
Case 3: -1

bfs+优先队列。。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::map;
15 using std::pair;
16 using std::vector;
17 using std::multimap;
18 using std::priority_queue;
19 #define pb(e) push_back(e)
20 #define sz(c) (int)(c).size()
21 #define mp(a, b) make_pair(a, b)
22 #define all(c) (c).begin(), (c).end()
23 #define iter(c) decltype((c).begin())
24 #define cls(arr,val) memset(arr,val,sizeof(arr))
25 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
26 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
28 const int N = 110;
29 typedef unsigned long long ull;
30 bool vis[N][N];
31 char trip[N][N];
32 const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 };
33 int R, C, Vs, Vp, Vt, Sx, Sy, Dx, Dy;
34 struct Node {
35     int x, y, s;
36     Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}
37     inline bool operator<(const Node &a) const {
38         return s > a.s;
39     }
40 };
41 int bfs() {
42     cls(vis, false);
43     priority_queue<Node> que;
44     que.push(Node(Sx, Sy, 0));
45     vis[Sx][Sy] = true;
46     while (!que.empty()) {
47         Node tmp = que.top(); que.pop();
48         if (tmp.x == Dx && tmp.y == Dy) return tmp.s;
49         rep(i, 4) {
50             int nx = dx[i] + tmp.x, ny = dy[i] + tmp.y;
51             char &ch = trip[nx][ny];
52             if (nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
53             if (ch == ‘@‘ || vis[nx][ny]) continue;
54             if (ch == ‘T‘) que.push(Node(nx, ny, tmp.s + Vt));
55             else if (ch == ‘.‘) que.push(Node(nx, ny, tmp.s + Vs));
56             else if (ch == ‘#‘) que.push(Node(nx, ny, tmp.s + Vp));
57             vis[nx][ny] = true;
58         }
59     }
60     return -1;
61 }
62 int main() {
63 #ifdef LOCAL
64     freopen("in.txt", "r", stdin);
65     freopen("out.txt", "w+", stdout);
66 #endif
67     int k = 1;
68     while (~scanf("%d %d", &R, &C)) {
69         scanf("%d %d %d", &Vp, &Vs, &Vt);
70         rep(i, R) scanf("%s", trip[i]);
71         scanf("%d %d %d %d", &Sx, &Sy, &Dx, &Dy);
72         printf("Case %d: %d\n", k++, bfs());
73     }
74     return 0;
75 }

时间: 2024-10-06 02:33:37

hdu 2425 Hiking Trip的相关文章

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)

[欧拉回路] hdu 3018 Ant Trip

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1658    Accepted Submission(s): 641 Problem Description Ant Country consist of N to

hdu 5360 Hiking(优先队列+贪心)

题目:http://acm.hdu.edu.cn/showproblem.php? pid=5360 题意:beta有n个朋友,beta要邀请他的朋友go hiking,已知每一个朋友的理想人数[L,R](现有L~R个人准备去,那么这个朋友就去). 求最多有多少人去. 及beta邀请朋友的顺序. 分析:每次邀请人的最优解就是:选会去的人里面R最小的那个人. 代码实现的话,cur代表已经准备go hiking的人数,每次将全部L<=cur的人放进优先队列,选出R最小的那个. 假设队列为空,那么剩下

HDU 5360 Hiking (贪心+优先队列)

本文纯属原创,转载注明出处:http://blog.csdn.net/zip_fan.谢谢. 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5360 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Special Judge Problem Description There are n soda conveniently la

hdu 3018 Ant Trip 欧拉回路+并查集

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,together with his friends,wants to go through every part

HDU 5360 Hiking(优先队列)

Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 492    Accepted Submission(s): 263 Special Judge Problem Description There are  soda conveniently labeled by . beta, their best friends,

HDU 5360——Hiking——————【贪心+优先队列】

Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 118    Accepted Submission(s): 69Special Judge Problem Description There are n soda conveniently labeled by 1,2,…,n. beta, their best fri

hdu 3018 Ant Trip 算是一道欧拉通路的题目吧~

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1826    Accepted Submission(s): 702 Problem Description Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,

HDU 3018 Ant Trip (欧拉路的个数 并查集)

Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5501    Accepted Submission(s): 2146 Problem Description Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,