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. uvm_env——UVM大环境(UVM Environment )

    1 What is uvm_env? uvm_env is used to create and connect the uvm_components like driver, monitors , ...

  2. C#遍历文件夹下全部文件

    public static List<string> GetFile(string path, List<string> FileList, string RelativePa ...

  3. (九)mybatis之生命周期

    生命周期   SqlSessionFactoryBuilder   SqlSessionFactoryBuilder的作用就是生成SqlSessionFactory对象,是一个构建器.所以我们一旦构建 ...

  4. App Store上的开源应用汇总

    以下是互联网上主要的开源iOS应用的列表,在学习的时候,多看看完成的功能代码可以给我们带来很多经验,但是除了Apple官方提供的Sample Code之外,我们很难找到优质的开源项目代码,所以我搜集了 ...

  5. vue2.0的变化

    1. 在每个组件模板,不在支持片段代码 组件中模板: 之前: <template> <h3>我是组件</h3><strong>我是加粗标签</st ...

  6. WPF知识点全攻略09- 附加属性

    附加属性也是一种特殊的依赖属性. Canvas中的Canvas.Left,Canvas.Top ,DockPanel中DockPanel.Dock等就是附加属性. 更加.NET类属性的写法经验.这个中 ...

  7. python之路——目录

    目录 python基础部分 基础部分 介绍.基本语法.流程控制 列表 元祖 字符串 字典 集合 文件操作 函数 变量 递归 迭代器,生成器,装饰器,Json和pickle 数据序列化 函数 初识函数 ...

  8. SC || 关于java迭代中修改迭代集合的操作

    在通过for循环遍历整个List/Map等的时候,如果想要进行remove的操作,这时就更改了迭代集合,会出现错误 一种方法是如果只会remove一个可以remove后直接break 另一种是把集合先 ...

  9. struts2的单个文件上传

    本文主要两种方式,一:通过 FileUtils.copyFile(file, savefile);方法复制:二:通过字节流方式复制 web.xml <?xml version="1.0 ...

  10. CSS - position属性小结

    Relative: 属于文档流,针对自身进行偏移: Absolute: 脱离文档流,针对最近的定位元素进行偏移,如果没有,则针对根元素,即body标签尽心偏移: Fixed: 和absolute基本一 ...