标签:acm 图论 zoj3877 earthstone keeper 双条件最短路
Description
Earthstone Keeper is a famous roguelike game created by Lizard Entertainment. In this game, an adventurer will explore in a single layer dungeon of N × M size.
The adventurer starts at the room (SR, SC) and he wants to go to the room located at (TR, TC) since there hides a big treasure box full of gold and diamonds! However, exploration is not always an easy job. There are many traps and monsters in the dungeon. To be specific, there are 4 types of rooms:
Two rooms are adjacent if and only if they share an edge. The adventurer can take 1 minute to go from a room to an adjacent room. Traps or monsters are always dangerous. More precisely, there is a fatality value for each trap and monster. Although our adventurer is strong and swift, battling with a deadly monster or dodging a rolling stone trap are not wise options.
The dungeon has been sealed by its owner with a powerful magic so the adventurer can not escape to outside until he found the treasure. By the way, being afraid of monsters battling with each other, the dungeon owner will not set any two monster rooms adjacent to each other. The room (SR, SC) and (TR, TC) are always common rooms and will not be adjacent to any monster room.
The adventurer want choose a best path to the treasure. The total fatality value of all monsters he killed and all traps he triggered should be as low as possible (If a trap was triggered multiple times, the fatality should also count multiple times). Among all safest paths, he want to choose the shortest path lead to the treasure room.
Please write program to help the adventurer find out the best path to the treasure.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers N and M (1 <= N, M <= 500). The second line contains 4 integers SR, SC, TR, TC (1 <= SR, TR <= N and 1 <= SC, TC <= M).
For the next N lines, each line contains M characters indicating the map of dungeon. Each type of room is marked as:
The fatality value of trap "a" and monster "A" is 1. The fatality value of trap "b" and monster "B" is 2, and so on. Therefore, the most dangerous trap "z" or monster "Z" has its fatality value equal to 26.
Output
For each test case, output the total fatality value and the time cost (in minutes) of the best path, separated by a space. It is guaranteed that there always exists at least one path from (SR, SC) to (TR, TC).
Sample Input
1 3 5 1 1 3 5 ..b.. .#C#. ..a..
Sample Output
4 6
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<functional> #include<utility> #include<cmath> using namespace std; const int MAXN = 502; //最大节点 const int MAXM = 1100000; //边的最大值 const int INF = 0x3f3f3f3f; char map[MAXN][MAXN]; //保存地图 pair<int, int> dis[MAXN][MAXN]; //用于最短路的长度(pair->first是伤害,second是距离) int n, m; int sx, sy, dx, dy; int dir[4][2] = { -1, 0, 1, 0, 0, -1, 0, 1 }; struct Edge { int vx, vy; int bl, dis; int next; }; //边结构 struct node { int x, y, b, d; node( int x, int y, int b, int d ) { this->x = x; this->y = y; this->b = b; this->d = d; } }; //节点状态结构 class cmp { public: bool operator() ( const node& lhs, const node& rhs ) { if(lhs.b != rhs.b) return lhs.b > rhs.b; return lhs.d > rhs.d; } }; int head[MAXN][MAXN]; Edge edge[MAXM]; int cnt = 0; void addedge( int ux, int uy, int vx, int vy, int bl, int dis ) { edge[cnt].vx = vx; edge[cnt].vy = vy; edge[cnt].bl = bl; edge[cnt].dis = dis; edge[cnt].next = head[ux][uy]; head[ux][uy] = cnt++; }//邻接表的数组实现 void ini( ) { memset( map, '#', sizeof map ); memset( head, -1, sizeof head ); cnt = 0; scanf( "%d%d", &n, &m ); scanf( "%d%d%d%d", &sx, &sy, &dx, &dy ); for(int i = 1; i <= n; i++) scanf( "%s", map[i] + 1 ); }//初始化函数 bool check( int x, int y ) { if(x <= 0 || y <= 0 || x > n || y > m || map[x][y] == '#')return false; return true; }//看看是否可以走到这个格子 void SPFA( ) //最短路算法,Dijkstra要超时 { priority_queue<node, vector<node>, cmp> q; q.push( node( sx, sy, 0, 0 ) ); memset( dis, 0x7f, sizeof dis ); dis[sx][sy] = make_pair( 0, 0 ); while(!q.empty( )) { int x = q.top( ).x; int y = q.top( ).y; if((x == dx) && (y == dy)) break; q.pop( ); for(int i = head[x][y]; i != -1; i = edge[i].next) { int vx = edge[i].vx; int vy = edge[i].vy; int temb = edge[i].bl; int temd = edge[i].dis; if(islower( map[vx][vy] )) temb += map[vx][vy] - 'a' + 1; if((dis[vx][vy].first > dis[x][y].first + temb) || (dis[vx][vy].first == dis[x][y].first + temb&&dis[vx][vy].second > dis[x][y].second + temd)) { dis[vx][vy].first = dis[x][y].first + temb; dis[vx][vy].second = dis[x][y].second + temd; q.push( node( vx, vy, dis[vx][vy].first, dis[vx][vy].second ) ); } } } } void create( ) //建图过程。比较复杂 { for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(map[i][j] != '#' && !isupper( map[i][j] )) //处理非怪物节点 { for(int k1 = 0; k1 <= 3; k1++) { int x1 = i + dir[k1][0]; int y1 = j + dir[k1][1]; if(!check( x1, y1 ) || isupper( map[x1][y1] )) continue; int blood = 0; for(int k2 = 0; k2 <= 3; k2++) { int x2 = x1 + dir[k2][0]; int y2 = y1 + dir[k2][1]; if(!check( x2, y2 )) continue; if(isupper( map[x2][y2] )) { blood += map[x2][y2] - 'A' + 1; } } addedge( i, j, x1, y1, blood, 1 ); } } //怪物节点特殊处理 for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(isupper( map[i][j] )) { for(int k1 = 0; k1 <= 3; k1++) { int x1 = i + dir[k1][0]; int y1 = j + dir[k1][1]; if(!check( x1, y1 ))continue; for(int k2 = 0; k2 <= 3; k2++) if(k2 != k1) { int x2 = i + dir[k2][0]; int y2 = j + dir[k2][1]; int blood = 0; if(!check( x2, y2 ))continue; for(int k3 = 0; k3 <= 3; k3++) { int x3 = x2 + dir[k3][0]; int y3 = y2 + dir[k3][1]; if(!check( x3, y3 ) || abs( x3 - x1 ) + abs( y3 - y1 ) <= 1) continue; if(isupper( map[x3][y3] )) blood += map[x3][y3] - 'A' + 1; } addedge( x1, y1, x2, y2, blood, 2 ); } } } } int main( ) { int kase; scanf( "%d", &kase ); while(kase--) { ini( ); create( ); SPFA( ); printf( "%d %d\n", dis[dx][dy].first, dis[dx][dy].second ); } return 0; }
解题报告 之 ZOJ3877 Earthstone Keeper
标签:acm 图论 zoj3877 earthstone keeper 双条件最短路
原文地址:http://blog.csdn.net/maxichu/article/details/45847151