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. github入门之基本操作--4

    1.初始化仓库 如果成功执行git init 命令,该目录下会生成一个.git的目录 2.查看仓库状态 *注: 实际工作中,git status使用次数非常多,一定要记住.因为当工作树和仓库被操作的过 ...

  2. poj 2411 Mondriaan's Dream (轮廓线DP)

    题意:有一个n*m的棋盘,要求用1*2的骨牌来覆盖满它,有多少种方案?(n<12,m<12) 思路: 由于n和m都比较小,可以用轮廓线,就是维护最后边所需要的几个状态,然后进行DP.这里需 ...

  3. HTML5资源汇总(更新游戏引擎cocos2d-html5)

    我也是现学现用,想了解的可以看看效果,想知道实现的也有源码 http://cocos2d-html5.org Cocos2d-HTML5 API和Cocos2d-x一致,同样的代码可以支持cocos2 ...

  4. 用例重试机制rerunfailures

    安装 rerunfailures插件 pip install pytest-rerunfailures 使用: pytest --reruns  重试次数 如:pytest --reruns  2 重 ...

  5. c++ 中的函数调用中的参数传递

    概述 初学 \(c++\),一直搞不懂其参数传递方式.故找到一篇不错的文章:刘志华的深入探讨C++语言中参数传递问题.亲自实践一遍,并作此记录,以加深印象.     主要内容 本文主要分为五个小部分, ...

  6. 一个小笔记(5):A*算法

    A-Star算法是一种静态路网中求解最短路径最有效的直接搜索方法其实百科有 http://baike.baidu.com/link?url=CvmkWQIAmztYgMq3Nk1WyWkDiC0koV ...

  7. Luogu P3627 抢掠计划

    题目传送门 \(Tarjan\)缩点+SPFA最长路 #include<iostream> #include<cstdio> #include<cstring> # ...

  8. windows下pycharm使用Anaconda安装包环境

    转自: https://www.cnblogs.com/heitaoq/p/8632315.html

  9. 排序算法C语言实现——冒泡、快排、堆排对比

    对冒泡.快排.堆排这3个算法做了验证,结果分析如下: 一.结果分析 时间消耗:快排 < 堆排 < 冒泡. 空间消耗:冒泡O(1) = 堆排O(1) < 快排O(logn)~O(n) ...

  10. 笔记-python-字符串格式化-format()

    笔记-python-字符串格式化-format() 1.      简介 本文介绍了python 字符串格式化方法format()的常规使用方式. 2.      使用 2.1.    Accessi ...