Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9698    Accepted Submission(s): 3165

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
 

主要看起点和终点那种点少,以少的为起点开搜。1A水过

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
const int N=210;
char pos[N][N];
int vis[N][N];
int n,m;
struct info
{
int x;
int y;
int time;
bool operator<(const info &a)
{
return time>a.time;
}
};
vector<info>kfc;
int total[2][200][200];
info direct[4]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}},S,T;
inline info operator+(const info &a,const info &b)
{
info c;
c.x=a.x+b.x;
c.y=a.y+b.y;
return c;
}
void init()
{
MM(pos);
MM(vis);
MMINF(total);
kfc.clear();
}
inline bool check(const info &a)
{
return (a.x>=0&&a.x<n&&a.y>=0&&a.y<m&&!vis[a.x][a.y]&&pos[a.x][a.y]!='#');
}
queue<info>Q;
int main(void)
{
int i,j,cnt;
while (~scanf("%d%d",&n,&m))
{
init();
cnt=0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
cin>>pos[i][j];
if(pos[i][j]=='Y')
{
S.x=i;
S.y=j;
S.time=0;
}
else if(pos[i][j]=='M')
{
T.x=i;
T.y=j;
T.time=0;
}
else if(pos[i][j]=='@')
{
info k;
k.x=i;
k.y=j;
kfc.push_back(k);
cnt++;
}
}
}
int c=0;
while (!Q.empty())
Q.pop();
Q.push(S);
vis[S.x][S.y]=1;
while (!Q.empty())
{
info now=Q.front();
Q.pop();
if(pos[now.x][now.y]=='@')
{
total[0][now.x][now.y]=now.time;
if(++c==cnt)
break;
}
for (i=0; i<4; i++)
{
info v=now+direct[i];
if(check(v))
{
v.time=now.time+11;
vis[v.x][v.y]=1;
Q.push(v);
}
}
}
while (!Q.empty())
Q.pop();
MM(vis);
c=0;
Q.push(T);
while (!Q.empty())
{
info now=Q.front();
Q.pop();
if(pos[now.x][now.y]=='@')
{
total[1][now.x][now.y]=now.time;
if(++c==cnt)
break;
}
for (i=0; i<4; i++)
{
info v=now+direct[i];
if(check(v))
{
v.time=now.time+11;
vis[v.x][v.y]=1;
Q.push(v);
}
}
}
int r=INF;
for (i=0; i<cnt; i++)
r=min(r,total[0][kfc[i].x][kfc[i].y]+total[1][kfc[i].x][kfc[i].y]);
printf("%d\n",r);
}
return 0;
}

HDU——2612Find a way(多起点多终点BFS)的更多相关文章

  1. Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)

    Key Vertex Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  2. HDU 2612 Find a way【多起点多终点BFS/两次BFS】

    Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  3. hdu 2066 多起点 多终点

    多起点 多终点 无向图 结点的个数要自己求 Sample Input6 2 3 //边数 起点数 终点数1 3 5 //u v w1 4 72 8 123 8 44 9 129 10 21 2 //起 ...

  4. HDU2066一个人的旅行---(多起点多终点最短路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=2066 一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memo ...

  5. dump 验证实例恢复的起点和终点

    什么时候会产生实例恢复呢?当你数据库服务器异常断电,重启数据库就会发生实例恢复.实例恢复是由数据库自动完成的,无须DBA的干涉.当然这里有个前提条件:数据文件. 在线日志文件.控制文件不得有损坏. 我 ...

  6. 【百度地图API】让用户选择起点和终点的驾车导航

    原文:[百度地图API]让用户选择起点和终点的驾车导航 摘要: 如果用户搜索“从机场到火车站”,使用驾车导航DrivingRoute会默认显示一条结果.但同一个城市可能有多个机场和火车站,那么,如何用 ...

  7. Coolest Ski Route-不定起点和终点----在有向变的情况下---求最长路

    这题最开始给你了N个点,M条边,边是单向边,问不指定起点和终点,最长路是什么??? 脑补一下,不定起点和终点的最短路,用弗洛伊德算法搞一搞,但是...那个垃圾算法的复杂度是N^3的,但是这个算法的M高 ...

  8. 【10.31校内测试】【组合数学】【记忆化搜索/DP】【多起点多终点二进制拆位Spfa】

    Solution 注意取模!!! Code #include<bits/stdc++.h> #define mod 1000000007 #define LL long long usin ...

  9. 【10.3校内测试【国庆七天乐!】】【DP+组合数学/容斥】【spfa多起点多终点+二进制分类】

    最开始想的暴力DP是把天数作为一个维度所以怎么都没有办法优化,矩阵快速幂也是$O(n^3)$会爆炸. 但是没有想到另一个转移方程:定义$f[i][j]$表示每天都有值的$i$天,共消费出总值$j$的方 ...

随机推荐

  1. LookAround开元之旅(持续更新中...)

    应用介绍随便瞧瞧是一款为android用户量身定做的免费图文资讯软件集美食,文学,语录等频道于一体界面简洁,操作流畅,图文分享,个性收藏是广大卓粉的必备神器APK下载 -->https://ra ...

  2. Git .gitignore 设置为全局global

    在操作Git时,我们会将修改的内容$git add . 到Git,Git会提示我们哪些文件都修改了.此时提示中会包括系统自动修改的文件,bin文件等.而我们add到Git时,并不希望将这些文件也一同a ...

  3. 洛谷 P1340 兽径管理

    题目描述 约翰农场的牛群希望能够在 N 个(1<=N<=200) 草地之间任意移动.草地的编号由 1到 N.草地之间有树林隔开.牛群希望能够选择草地间的路径,使牛群能够从任一 片草地移动到 ...

  4. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource

    spring boot web项目运行时提示如下错误 org.springframework.beans.factory.BeanCreationException: Error creating b ...

  5. python之路——递归函数

    阅读目录 楔子 初识递归 再谈递归 递归函数与三级菜单 递归函数与二分查找算法 楔子 在讲今天的内容之前,我们先来讲一个故事,讲的什么呢?从前有座山,山里有座庙,庙里有个老和尚讲故事,讲的什么呢?从前 ...

  6. .net MVC下跨域Ajax请求(CORS)

    二.CROS (Cross-origin Resource Sharing) CROS相当于一种协议,由浏览器.服务端共同完成安全验证,进行安全的跨域资源共享.对于开发人员来说就跟在本站AJAX请求一 ...

  7. file-leak-detector(文件句柄泄漏)在JDK1.6环境下 weblogic 和 tomcat安装方式以及使用方式

    file-leak-detector作者博客详见: http://file-leak-detector.kohsuke.org/ file-leak-detector学习贴: https://blog ...

  8. python常用模块之json和pickle模块

    json模块 json.dumps     将 Python 对象编码成 JSON 字符串 json.loads       用于解码 JSON 数据.该函数返回 Python 字段的数据类型. pi ...

  9. 一款App的开发成本是多少?

    答一: 接触过上万名创业者,开发上线过超过30款App,没有比我更适合回答这个问题的了.. 本文对想做好一款App项目的人来说这是一篇价值百万的回答!因为这是我们花了几百万试错成本试出来的经验! &l ...

  10. jExcelAPI导入导出excel

      MS的电子表格(Excel)是Office的重要成员,是保存统计数据的一种常用格式.作为办公文档,势必要涉及到的电子文档的交换,Excel是一种在企业中非常通用的文件格式,打印和管理也比较方便.在 ...