hnu 12921 Enterprising Escape

来源:互联网 发布:mac 因为找不到原身 编辑:程序博客网 时间:2024/05/22 01:37

Enterprising EscapeTime Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KBTotal submit users: 28, Accepted users: 18Problem 12921 : No special judgementProblem description

The Enterprise is surrounded by Klingons! Find the escape route that has the quickest exit time, and print that time.

Input is a rectangular grid; each grid square either has the Enterprise or some class of a Klingon warship. Associated with each class of Klingon warship is a time that it takes for the Enterprise to defeat that Klingon. To escape, the Enterprise must defeat each Klingon on some path to the perimeter. Squares are connected by their edges, not by corners (thus, four neighbors).

Input

The first line will contain T, the number of cases; 2 ≤ T ≤ 100. Each case will start with line containing three numbers k, w, and h. The value for k is the number of different Klingon classes and will be between 1 and 25, inclusive. The value for w is the width of the grid and will be between 1 and 1000, inclusive. The value for h is the height of the grid and will be between 1 and 1000, inclusive.

Following that will be k lines. Each will consist of a capital letter used to label the class of Klingon ships followed by the duration required to defeat that class of Klingon. The label will not be “E”. The duration is in minutes and will be between 0 and 100,000, inclusive. Each label will be distinct.

Following that will be h lines. Each will consist of w capital letters (with no spaces between them). There will be exactly one “E” across all h lines, denoting the location of the Enterprise; all other capital letters will be one of the k labels given above, denoting the class of Klingon warship in the square.

Output

Your output should be a single integer value indicating the time required for the Enterprise to escape.

Sample Input
26 3 3A 1B 2C 3D 4F 5G 6ABCFECDBG2 6 3A 100B 1000BBBBBBAAAAEBBBBBBB
Sample Output
2400

 题目没看  就是知道大概的意思   就是会有K个字母 每个字母对应相应的值 在一个地图中  从E字母开始出发 直到最外面记录走过的点的值  求走到外面值的最小值   

第一反映就是优先队列+bfs  写起来还算简单 因为只有大写字母 直接用数组存储点的值就好   遍历过的点要标记

代码如下:

<span style="font-size:18px;">#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <queue>#define eps 1e-8#define op operator#define MOD  10009#define MAXN  100100#define FOR(i,a,b)  for(int i=a;i<=b;i++)#define FOV(i,a,b)  for(int i=a;i>=b;i--)#define REP(i,a,b)  for(int i=a;i<b;i++)#define REV(i,a,b)  for(int i=a-1;i>=b;i--)#define MEM(a,x)    memset(a,x,sizeof a)#define ll __int64using namespace std;int num[26];bool vis[1005][1005];char map[1005][1005];int sx,sy;int dir[4][2]={{0,-1},{1,0},{0,1},{-1,0}};int k,w,h;struct node{    int x,y;    int sum;    bool operator <(const node p)const    {        return sum>p.sum;    }};node p,q;void bfs(){    priority_queue<node> Q;    q.x=sx; q.y=sy;    q.sum=0;    vis[q.x][q.y]=1;    Q.push(q);    while(!Q.empty())    {        q=Q.top(); Q.pop();//        cout<<q.x<<"  "<<q.y<<"  "<<q.sum<<endl;        if(q.x<0||q.x>=h||q.y<0||q.y>=w)        {            printf("%d\n",q.sum);//            cout<<"ww"<<w<<"hh"<<h<<endl;//            cout<<"11111111111"<<endl;            return;        }        for(int i=0;i<4;i++)        {            p.x=q.x+dir[i][0]; p.y=q.y+dir[i][1];            if(p.x<0||p.x>=h||p.y<0||p.y>=w)            {                printf("%d\n",q.sum);                return;            }            if(!vis[p.x][p.y])            {                int tt=map[p.x][p.y]-'A';                vis[p.x][p.y]=1;                p.sum=q.sum+num[tt];                Q.push(p);            }        }    }}int main(){//freopen("ceshi.txt","r",stdin);    int tc;    scanf("%d",&tc);    while(tc--)    {        scanf("%d%d%d\n",&k,&w,&h);        MEM(vis,0);        for(int i=0;i<k;i++)        {            char c; int n;            scanf("%c %d\n",&c,&n);            num[c-'A']=n;        }        for(int i=0;i<h;i++)        {            scanf("%s",map[i]);            for(int j=0;j<w;j++)                if(map[i][j]=='E')                {                    sx=i; sy=j;                }        }        bfs();    }    return 0;}</span>




0 0