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

hdu 2612 多终点BFS

时间:2017-07-16 15:49:41      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:sam   each   tom   相加   pass   size   names   let   char   

Find a way

Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
 

 

Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200). 
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
 

 

Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
 

 

Sample Input
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
 

 

Sample Output
66 88 66
 

思路:两次BFS存下对每个kfc的最短距离,之后两两相加取min

代码:

 1 #include "cstdio"
 2 #include "stdlib.h"
 3 #include "iostream"
 4 #include "algorithm"
 5 #include "string"
 6 #include "cstring"
 7 #include "queue"
 8 #include "cmath"
 9 #include "vector"
10 #include "map"
11 #include "set"
12 #define mj
13 #define db double
14 #define ll long long
15 using namespace std;
16 const int N=1e8+2;
17 const int mod=1e9+7;
18 //const ll inf=1e16+10;
19 #define inf 0x3f3f3f
20 typedef pair<int,int> P;
21 int n,m;
22 char  s[300][300];
23 int d[300][300],k[205][205];
24 int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
25 int t[2][205*205];
26 int bfs(int sx,int sy,int id)
27 {
28     queue<P> q;
29     for(int i=0;i<205;i++){
30         for(int j=0;j<205;j++){
31             d[i][j]=N;
32         }
33     }
34     q.push(P(sx,sy));
35     d[sx][sy]=0;
36     while(q.size()){
37         P p;
38         p=q.front(),q.pop();
39         for(int i=0;i<4;i++){
40             int nx=p.first+dx[i],ny=p.second+dy[i];
41             if(0<=nx&&nx<n&&0<=ny&&ny<m&&s[nx][ny]!=#&&d[nx][ny]==N){
42                 d[nx][ny]=d[p.first][p.second]+1;
43                 if(s[nx][ny]==@) t[id][k[nx][ny]]=d[nx][ny];//到该点的距离
44                 q.push(P(nx,ny));
45             }
46         }
47     }
48     return 0;
49 }
50 int main()
51 {
52     int xx[3],yy[3];
53     while(scanf("%d%d",&n,&m)==2){
54         memset(t,inf, sizeof(t));
55         int c=0,cnt=0,ma=N;
56         for(int i=0;i<n;i++){
57             scanf("%s",s[i]);
58             for(int j=0;j<m;j++){
59                 if(s[i][j]==Y) xx[c]=i,yy[c++]=j;
60                 else if(s[i][j]==M) xx[c]=i,yy[c++]=j;
61                 else if(s[i][j]==@){
62                     k[i][j]=cnt++;
63                 }
64             }
65         }
66         bfs(xx[0],yy[0],0),bfs(xx[1],yy[1],1);
67         for(int i=0;i<cnt;i++){
68             ma=min(t[0][i]+t[1][i],ma);
69 //            printf("%d %d\n",t[0][i],t[1][i]);
70         }
71         printf("%d\n",11*ma);
72     }
73     return 0;
74 }

 

hdu 2612 多终点BFS

标签:sam   each   tom   相加   pass   size   names   let   char   

原文地址:http://www.cnblogs.com/mj-liylho/p/7190577.html

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