码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4308 最短路或者bfs

时间:2015-03-31 00:27:27      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

Saving Princess claire_

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2435    Accepted Submission(s): 864


Problem Description
Princess claire_ was jailed in a maze by Grand Demon Monster(GDM) teoy.
Out of anger, little Prince ykwd decides to break into the maze to rescue his lovely Princess.
The maze can be described as a matrix of r rows and c columns, with grids, such as ‘Y‘, ‘C‘, ‘*‘, ‘#‘ and ‘P‘, in it. Every grid is connected with its up, down, left and right grids.
There is only one ‘Y‘ which means the initial position when Prince ykwd breaks into the maze.
There is only one ‘C‘ which means the position where Princess claire_ is jailed.
There may be many ‘*‘s in the maze, representing the corresponding grid can be passed through with a cost of certain amount of money, as GDM teoy has set a toll station.
The grid represented by ‘#‘ means that you can not pass it. 
It is said that as GDM teoy likes to pee and shit everywhere, this grid is unfortunately damaged by his ugly behavior.
‘P‘ means it is a transmission port and there may be some in the maze. These ports( if exist) are connected with each other and Prince ykwd can jump from one of them to another. 

They say that there used to be some toll stations, but they exploded(surely they didn‘t exist any more) because of GDM teoy‘s savage act(pee and shit!), thus some wormholes turned into existence and you know the following things. Remember, Prince ykwd has his mysterious power that he can choose his way among the wormholes, even he can choose to ignore the wormholes.
Although Prince ykwd deeply loves Princess claire_, he is so mean that he doesn‘t want to spend too much of his money in the maze. Then he turns up to you, the Great Worker who loves moving bricks, for help and he hopes you can calculate the minimum money he needs to take his princess back.
 

 

Input
Multiple cases.(No more than fifty.)
The 1st line contains 3 integers, r, c and cost. ‘r‘, ‘c‘ and ‘cost‘ is as described above.(0 < r * c <= 5000 and money is in the range of (0, 10000] )
Then an r * c character matrix with ‘P‘ no more than 10% of the number of all grids and we promise there will be no toll stations where the prince and princess exist.
 

 

Output
One line with an integer, representing the minimum cost. If Prince ykwd cannot rescue his princess whatever he does, then output "Damn teoy!".(See the sample for details.)
 

 

Sample Input
1 3 3
Y*C
1 3 2
Y#C
1 5 2
YP#PC
 

 

Sample Output
3
Damn teoy!
0
 

 

Author
BUPT
 

 

Source
 
 
题目意思:
给一个n*m的图,从Y点开始走,走到C点,*表示收费,#不可走,P表示传送门,P之间任意传送或者不传送,求最小的费用。
 
思路:
一看到这道题目直接就想到最短路了,建图spfa搞定。
在网上看别人代码大部分是bfs,仔细一想,这道题可以用bfs来做,因为图上没有空地,若有空地的话只能用最短路了。
bfs代码就不贴了。
 
这道题数据有点问题:
图上最多有5000*2条边,每条边是双向的,那么就是5000*2*2了,然后最多500个p点,p点两两相连、
也就500*500*2条边了。那么图上加起来建5000*4+500000条边就可以了。
但是WA了,改成5000*4+1500000就A了,我也不知道为什么。。。难道我读题错误?。。。
代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <cmath>
  8 using namespace std;
  9 
 10 #define N 5005
 11 #define inf 999999999
 12 
 13 int max(int x,int y){return x>y?x:y;}
 14 int min(int x,int y){return x<y?x:y;}
 15 int abs(int x,int y){return x<0?-x:x;}
 16 
 17 char map[N][N];
 18 int dis[N];
 19 bool visited[N];
 20 
 21 struct Edge{
 22     int u, v, w, next;
 23 }a[N*4+1500000];
 24 
 25 
 26 int cnt;
 27 int head[N];
 28 int p[505];
 29 int n, m, cost;
 30 
 31 void setEdge(int u,int v,int w){
 32     a[cnt].u=u;a[cnt].v=v;a[cnt].w=w;
 33     a[cnt].next=head[u];head[u]=cnt++;
 34 }
 35 
 36 
 37 void spfa(int st){
 38     queue<int>Q;
 39     int i, j, k;
 40     memset(visited,false,sizeof(visited));
 41     for(i=0;i<n*m;i++) dis[i]=inf;
 42     dis[st]=0;
 43     visited[st]=true;
 44     Q.push(st);
 45     int u, v;
 46     while(!Q.empty()){
 47         u=Q.front();Q.pop();visited[u]=false;
 48         for(i=head[u];i!=-1;i=a[i].next){
 49             if(dis[a[i].v]>dis[u]+a[i].w){
 50                 dis[a[i].v]=dis[u]+a[i].w;
 51                 if(!visited[a[i].v]){
 52                     Q.push(a[i].v);
 53                     visited[a[i].v]=true;
 54                 }
 55             }
 56         }
 57     }
 58 }
 59 
 60 main()
 61 {
 62     int i, j, k;
 63     while(scanf("%d %d %d",&n,&m,&cost)==3){
 64         for(i=0;i<n;i++) scanf("%s",map[i]);
 65         memset(head,-1,sizeof(head));
 66         int cnt=0;
 67         int st, end;
 68         k=0;
 69         for(i=0;i<n;i++){
 70             for(j=0;j<m;j++){
 71                 if(map[i][j]==#) continue;
 72                 if(map[i][j]==Y) st=i*m+j;
 73                 else if(map[i][j]==C) end=i*m+j;
 74                 else if(map[i][j]==P) p[k++]=i*m+j;
 75                 if(i<n-1&&map[i+1][j]!=#){
 76                     if(map[i+1][j]==*){
 77                         setEdge(i*m+j,(i+1)*m+j,cost);
 78                         if(map[i][j]==*){
 79                             setEdge((i+1)*m+j,i*m+j,cost);
 80                         }
 81                         else{
 82                             setEdge((i+1)*m+j,i*m+j,0);
 83                         }
 84                     }
 85                     else {
 86                         setEdge(i*m+j,(i+1)*m+j,0);
 87                         if(map[i][j]==*){
 88                             setEdge((i+1)*m+j,i*m+j,cost);
 89                         }
 90                         else{
 91                             setEdge((i+1)*m+j,i*m+j,0);
 92                         }
 93                     }
 94                 }
 95                 if(j<m-1&&map[i][j+1]!=#){
 96                     if(map[i][j+1]==*){
 97                         setEdge(i*m+j,i*m+j+1,cost);
 98                         if(map[i][j]==*){
 99                             setEdge(i*m+j+1,i*m+j,cost);
100                         }
101                         else{
102                             setEdge(i*m+j+1,i*m+j,0);
103                         }
104                     }
105                     else {
106                         setEdge(i*m+j,i*m+j+1,0);
107                         if(map[i][j]==*){
108                             setEdge(i*m+j+1,i*m+j,cost);
109                         }
110                         else{
111                             setEdge(i*m+j+1,i*m+j,0);
112                         }
113                     }
114                 }
115             }
116         }
117         for(i=0;i<k;i++){
118             for(j=i+1;j<k;j++){
119                 setEdge(p[i],p[j],0);
120                 setEdge(p[j],p[i],0);
121             }
122         }
123         spfa(st);
124         if(dis[end]==inf) printf("Damn teoy!\n");
125         else printf("%d\n",dis[end]);
126     }
127 }

 

HDU 4308 最短路或者bfs

标签:

原文地址:http://www.cnblogs.com/qq1012662902/p/4379488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!