http://www.lydsy.com/JudgeOnline/problem.php?id=3402

又是spfa水题。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=20005, Q=N*10, oo=~0u>>2;
int q[Q], front, tail, d[N], vis[N], ihead[N], cnt, n, m;
struct ED { int to, next; }e[Q];
void add(int u, int v) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;
}
void spfa() {
q[tail++]=1;
for1(i, 2, n) d[i]=oo;
vis[1]=1;
while(front!=tail) {
int v, u=q[front++]; if(front==Q) front=0; vis[u]=0;
for(int i=ihead[u]; i; i=e[i].next) if(d[v=e[i].to]>d[u]+1) {
d[v]=d[u]+1;
if(!vis[v]) {
vis[v]=1;
q[tail++]=v; if(tail==Q) tail=0;
}
}
}
} int main() {
read(n); read(m);
for1(i, 1, m) {
int u=getint(), v=getint();
add(u, v);
}
spfa();
int mx=0, ans=0, id=oo;
for1(i, 1, n) mx=max(d[i], mx);
for1(i, 1, n) if(d[i]==mx) { ++ans; id=min(i, id); }
printf("%d %d %d", id, mx, ans);
return 0;
}

Description

    贝茜在和约翰玩一个“捉迷藏”的游戏.
    她正要找出所有适合她躲藏的安全牛棚.一共有 N(2≤N≤20000)个牛棚,被编为1到N号.她知道约翰(捉牛者)从牛棚1出发.所有的牛棚由M(1≤M≤50000)条双向路连接,每条双向路连 接两个不同的牛棚.所有的牛棚都是相通的.贝茜认为同牛棚1距离最远的的牛棚是安全的.两个牛棚间的距离是指,从一个牛棚到另一个牛棚最少需要通过的道路 数量.请帮贝茜找出所有的安全牛棚.

Input

    第1行输入两个整数N和M,之后M行每行输入两个整数,表示一条路的两个端点.
   

Output

仅一行,输出三个整数.第1个表示安全牛棚(如果有多个,输出编号最小的);第2个表示牛棚1和安全牛棚的距离;第3个表示有多少个安全的牛棚.

Sample Input

6 7
3 6
4 3
3 2
1 3
1 2
2 4
5 2

Sample Output

4 2 3

HINT

Source

【BZOJ】3402: [Usaco2009 Open]Hide and Seek 捉迷藏(spfa)的更多相关文章

  1. BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    题目 3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MB Description     贝 ...

  2. BZOJ—— 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    http://www.lydsy.com/JudgeOnline/problem.php?id=3402 Description     贝茜在和约翰玩一个“捉迷藏”的游戏.     她正要找出所有适 ...

  3. BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏(最短路)

    这个= =一看就是最短路了= = PS:最近有点懒 = = 刚才看到一道平衡树的裸题还嫌懒不去写= =算了等刷完这堆水题再去理= = CODE: #include<cstdio>#incl ...

  4. 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 78  Solved: 6 ...

  5. BZOJ3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 4 ...

  6. B3402 [Usaco2009 Open]Hide and Seek 捉迷藏 最短路

    直接最短路板子,dij堆优化. 题干: 题目描述 贝茜在和约翰玩一个“捉迷藏”的游戏. 她正要找出所有适合她躲藏的安全牛棚.一共有N(≤N≤)个牛棚,被编为1到N号.她知道约翰(捉牛者)从牛棚1出发. ...

  7. bzoj 1576: [Usaco2009 Jan]安全路经Travel【spfa+树链剖分+线段树】

    这几天写USACO水题脑子锈住了--上来就贪心,一交就WA 事实上这个是一个叫最短路树的东西,因为能保证只有一条最短路,所以所有最短路合起来是一棵以1为根的树,并且在这棵树上,每个点被精灵占据的路是它 ...

  8. 【BZOJ】【1941】【SDOI2010】Hide and Seek

    KD-Tree 一开始看错题了

  9. bzoj:1941: [Sdoi2010]Hide and Seek

    1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec  Memory Limit: 162 MBSubmit: 531  Solved: 295[Submi ...

随机推荐

  1. python 学习定时任务apscheduler模块

    最近在解决定时任务问题找到了apscheduler模块,贴一段代码 from apscheduler.schedulers.blocking import BlockingSchedulerimpor ...

  2. 搭建Vue环境总是出错,就重新安装就好了

    总是报错,还不如重新安装.. 错误千奇百怪,解决了 这个错误又会出现另外一个. 百度了一个挺好用的e 在window下搭建Vue.Js开发环境   nodejs官网http://nodejs.cn/下 ...

  3. An error occured while handling a json request

    修复方法: sudo pip install werkzeug==0.8.3

  4. CentOS下febootstrap自制Docker的CentOS6.6和7.1 Docker镜像

    docker image centos febootstrap CentOS 6.6和7.1 Docker自制CentOS镜像 安装: ? 1 yum -y install febootstrap 添 ...

  5. Ubuntu下安装JDK7(附Clojure下载)

    转:http://www.linuxidc.com/Linux/2012-10/71557.htm 首先确实不得不说,网上有很多类似Ubuntu下安装JDK7的教程.不过大都是基于JDK6的bin文件 ...

  6. ZOJ - 3725 Painting Storages

    Description There is a straight highway with N storages alongside it labeled by 1,2,3,...,N. Bob ask ...

  7. log4j 配置(转)

    log4j是干什么的 log4j是Apache的一个开源项目,主要功能是打印日志信息,以各种形式在各种地方花式打印日志. 使用log4j的准备工作 使用log4j就必须要引入其jar包.附上官网地址h ...

  8. 采集Snoopy.class.php

    <?php /************************************************* Snoopy - the PHP net client Author: Mont ...

  9. REMOTE HOST IDENTIFICATION HAS CHANGED问题的解决方式

    好久没更新博客园. 这段没更新博客的时间内收获了很多,所以更新下博客来整理.记录这段时间内学到的内容. 最近腾讯云服务器欠费停机了,所以趁着缴费.趁着心血来潮就……重装了云系统.结果在进行远程ssh连 ...

  10. 运行第一个.net core程序

    前置条件 ubuntu已安装.net core运行环境 分6步 mkdir netcore 创建一个项目文件夹 cd netcore   进入该文件夹 dotnet new  new命令 用于创建一个 ...