hdu 4308 Saving Princess claire_

来源:互联网 发布:linux 查看用户组 编辑:程序博客网 时间:2024/05/22 04:31
#include<iostream>#include<vector>#include<algorithm>#include<cstdio>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<cmath>#include<cassert>#include<cstring>#include<iomanip>using namespace std;#ifdef _WIN32#define i64 __int64#define out64 "%I64d\n"#define in64 "%I64d"#else#define i64 long long#define out64 "%lld\n"#define in64 "%lld"#endif/************ for topcoder by zz1215 *******************/#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)#define FFF(i,a)        for( int i = 0 ; i < (a) ; i ++)#define FFD(i,a,b)      for( int i = (a) ; i >= (b) ; i --)#define S64(a)          scanf(in64,&a)#define SS(a)           scanf("%d",&a)#define LL(a)           ((a)<<1)#define RR(a)           (((a)<<1)+1)#define pb              push_back#define CL(Q)           while(!Q.empty())Q.pop()#define MM(name,what)   memset(name,what,sizeof(name))#define read            freopen("in.txt","r",stdin)#define write           freopen("out.txt","w",stdout)const int inf = 0x3f3f3f3f;const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;const double oo = 10e9;const double eps = 10e-9;const double pi = acos(-1.0);const int maxn = 5011;const int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};struct zz{int x;int y;}zx;vector<zz>v;i64 n,m,c;string s[maxn];vector<i64>way[maxn];vector<bool>inq[maxn];int sx,sy;int ex,ey;bool yes(int x,int y){if(x>=0 && x<n && y>=0 && y<m){return true;}return false;}void spfa(){deque<zz>q;q.clear();zx.x = sx;zx.y = sy;way[sx][sy] = 0;q.push_back(zx);inq[sx][sy] = true;zz now,to;i64 temp;while(!q.empty()){now = q.front();q.pop_front();for(int i=0;i<4;i++){to.x = now.x + dir[i][0];to.y = now.y + dir[i][1];if(!yes(to.x,to.y) || s[to.x][to.y] == '#'){continue;}if(s[to.x][to.y]=='*'){temp = way[now.x][now.y] + c;}else{temp = way[now.x][now.y];}if(temp < way[to.x][to.y]){way[to.x][to.y] = temp;if(!inq[to.x][to.y]){inq[to.x][to.y] = true;if(!q.empty() && temp <= way[q.front().x][q.front().y]){q.push_front(to);}else{q.push_back(to);}}}}if(s[now.x][now.y] == 'P'){temp = way[now.x][now.y];for(int i=0;i<v.size();i++){to = v[i];if(way[to.x][to.y] > temp){way[to.x][to.y] = temp;if(!inq[to.x][to.y]){inq[to.x][to.y] = true;                        if(!q.empty() && temp <= way[q.front().x][q.front().y]){q.push_front(to);}else{q.push_back(to);}}}}}inq[now.x][now.y] = false;}return;}int main(){while(cin>>n>>m>>c){v.clear();for(int i=0;i<n;i++){way[i].clear();inq[i].clear();for(int j=0;j<m;j++){way[i].pb(inf64);inq[i].pb(false);}}for(int i=0;i<n;i++){cin>>s[i];}for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(s[i][j]=='P'){zx.x = i;zx.y = j;v.pb(zx);}if(s[i][j]=='Y'){sx = i;sy = j;}if(s[i][j]=='C'){ex = i;ey = j;}}}spfa();if(way[ex][ey] == inf64){cout<<"Damn teoy!"<<endl;}else{cout<<way[ex][ey]<<endl;}}    return 0;}