洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek

洛谷传送门

JDOJ 2641: USACO 2009 Open Silver 1.Hide and Seek

JDOJ传送门

题目描述

Bessie is playing hide and seek (a game in which a number of players hide and a single player (the seeker) attempts to find them after which various penalties and rewards are assessed; much fun usually ensues).

She is trying to figure out in which of N (2 <= N <= 20,000) barns conveniently numbered 1..N she should hide. She knows that FJ (the seeker) starts out in barn 1. All the barns are connected by M (1 <= M <= 50,000) bidirectional paths with endpoints A_i and B_i (1 <= A_i <= N; 1 <= B_i <= N; A_i != B_i); it is possible to reach any barn from any other through the paths.

Bessie decides that it will be safest to hide in the barn that has the greatest distance from barn 1 (the distance between two barns is the smallest number of paths that one must traverse to get from one to the other). Help Bessie figure out the best barn in which to hide.

奶牛贝西和农夫约翰(FJ)玩捉迷藏,现在有N个谷仓,FJ开始在第一个谷仓,贝西为了不让FJ找到她,当然要藏在距离第一个谷仓最远的那个谷仓了。现在告诉你N个谷仓,和M个两两谷仓间的“无向边”。每两个仓谷间当然会有最短路径,现在要求距离第一个谷仓(FJ那里)最远的谷仓是哪个(所谓最远就是距离第一个谷仓最大的最短路径)?如有多个则输出编号最小的。以及求这最远距离是多少,和有几个这样的谷仓距离第一个谷仓那么远。

输入格式

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Line i+1 contains the endpoints for path i: A_i and B_i

第一行:两个整数N,M;

第2-M+1行:每行两个整数,表示端点A_i 和 B_i 间有一条无向边。

输出格式

* Line 1: On a single line, print three space-separated integers: the index of the barn farthest from barn 1 (if there are multiple such barns, print the smallest such index), the smallest number of paths needed to reach this barn from barn 1, and the number of barns with this number of paths.

仅一行,三个整数,两两中间空格隔开。表示:距离第一个谷仓最远的谷仓编号(如有多个则输出编号最小的。),以及最远的距离,和有几个谷仓距离第一个谷仓那么远。

Sample Input

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

Sample Output

4 2 3

SPFA的一道模板题,怎么说呢?应该是模板题加深了一点点。

首先我们理解好这题的三个问:

第一问,最长路谷仓编号。这里的最长路是最大的最短路。

第二问,最长路是多少。

第三问,有几条最长路。

然后就没有然后了。

先跑SPFA得出一个f数组存储从源点1开始到i点的最短路。

然后从头枚举,碰到更长的最短路就更新+记录,记录编号(ans)和总数(cnt),就可以轻松AC啦!

上代码:

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int n,m,lst,ans,cnt;
int tot,to[100001],nxt[100001],head[20001];
void add(int x,int y)
{
to[++tot]=y;
nxt[tot]=head[x];
head[x]=tot;
}
int f[20001],v[20001];
void spfa()
{
memset(f,0x3f,sizeof(f));
memset(v,0,sizeof(v));
queue<int> q;
q.push(1);
f[1]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
v[x]=0;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(f[y]>f[x]+1)
{
f[y]=f[x]+1;
if(v[y]==0)
q.push(y),v[y]=1;
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
spfa();
for(int i=1;i<=n;i++)
{
if(f[i]>lst)
{
lst=f[i];
ans=i;
cnt=1;
}
else if(f[i]==lst)
cnt++;
}
printf("%d %d %d",ans,lst,cnt);
return 0;
}

USACO Hide and Seek的更多相关文章

  1. 【BZOJ-1941】Hide and Seek KD-Tree

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

  2. [BZOJ1941][Sdoi2010]Hide and Seek

    [BZOJ1941][Sdoi2010]Hide and Seek 试题描述 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他 ...

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

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

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

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

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

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

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

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

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

    KD-Tree 一开始看错题了

  8. 洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek

    题目戳 题目描述 Bessie is playing hide and seek (a game in which a number of players hide and a single play ...

  9. 【BZOJ1941】Hide and Seek(KD-Tree)

    [BZOJ1941]Hide and Seek(KD-Tree) 题面 BZOJ 洛谷 题解 \(KD-Tree\)对于每个点搜一下最近点和最远点就好了 #include<iostream> ...

随机推荐

  1. oracle--DG初始化参数

    下列参数为Primary角色相关的初始化参数 DB_NAME 注意保持同一个Data Guard中所有数据库DB_NAME相同 例如:DB_NAME=kingle DB_UNIQUE_NAME 为每一 ...

  2. [转载]3.1 UiPath鼠标操作元素的介绍和使用

    一.鼠标(mouse)操作的介绍 模拟用户使用鼠标操作的一种行为,例如单击,双击,悬浮.根据作用对象的不同我们可以分为对元素的操作.对文本的操作和对图像的操作 二.鼠标对元素的操作在UiPath中的使 ...

  3. mysql Duplicate entry '9223372036854775807' for key 'PRIMARY'

    mysql插入数据报错提示: ERROR 1062(23000) Duplicate entry  '9223372036854775807' for key 'PRIMARY' 发现问题果断 直接 ...

  4. SQLAlchemy基础

    1.介绍 做个简单笔记,方便回顾. SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后 ...

  5. [JAVA] 日常填坑 java.lang.SecurityException: Prohibited package name: java.xxx

    java虚拟机不允许包名以java开头. https://blog.csdn.net/sinat_28690417/article/details/72328547

  6. 更新element-ui版本

    1. 卸载当前版本 npm uninstall element-ui 2. 安装指定版本 npm -S

  7. python程序设计基础(程序设计基础方法)

    python初学者程序练习题 注:练习题涉及到range()函数的使用方法和python绘制,后面会单独发篇解释说明. 1.字符串拼接.接收用户输入的两个字符串,将它们组合后输出 str1=input ...

  8. javascript(六)运算符

    运算符概述 JavaScript中的运算符用于算术表达式. 比较表达式. 逻辑表达式. 赋值表达式等.需要注意的是, 大多数运算符都是由标点符号表示的, 比如 "+" 和" ...

  9. Java I/O系统学习系列三:I/O流的典型使用方式

    尽管可以通过不同的方式组合IO流类,但我们可能也就只用到其中的几种组合.下面的例子可以作为典型的IO用法的基本参考.在这些示例中,异常处理都被简化为将异常传递给控制台,但是这只有在小型示例和工具中才适 ...

  10. 获取apache ignite缓存中的数据行数少于实际行数

    我将ignite项目打包放到linux下,在linux下获取window中存放在oracle数据库中的数据,linux服务器作为ignite的服务端节点,我在本地启动tomact,作为ignite客户 ...