题目描述

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.

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

输入输出样例

输入样例#1:

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

输出样例#1:

4 2 3

说明

The farm layout is as follows:

Barns 4, 5, and 6 are all a distance of 2 from barn 1. We choose barn 4 because it has the smallest index.

这里谷仓4,5,6距离1号谷仓都是2,但是4编号最小所以输出4.因此最远距离是2且有3个谷仓,依次输出:2和3。

感谢 wjcwinmt 的贡献翻译

最短路

堆优化dijkstra练习

屠龙宝刀点击就送

#include <algorithm>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <queue>
#define N 100005
using namespace std;
struct node
{int x,y;bool operator<(node a)const{return y>a.y;}
};
struct dist
{int num,dis;bool operator<(dist b)const{if(dis==b.dis) return num<b.num;return dis>b.dis;}
}p[N];
priority_queue<node>q;
inline void read(int &x)
{x=0;bool f=0;register char ch=getchar();for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;for(; isdigit(ch);ch=getchar()) x=x*10+ch-'0';x=f?-x:x;
}
bool vis[N];
int n,m,head[N],to[N],Next[N],cnt;
int main()
{freopen("hideseek.in","r",stdin);freopen("hideseek.out","w",stdout);read(n);read(m);for(int x,y;m--;){read(x);read(y);Next[++cnt]=head[x];to[cnt]=y;head[x]=cnt;Next[++cnt]=head[y];to[cnt]=x;head[y]=cnt;}for(int i=1;i<=n;i++) p[i].num=i,p[i].dis=0x7ffffff;p[1].dis=0;node a;a.x=1;a.y=p[1].dis;q.push(a);while(!q.empty()){node a=q.top();q.pop();if(vis[a.x]) continue;vis[a.x]=1;for(int i=head[a.x];i;i=Next[i]){int v=to[i];if(p[v].dis>p[a.x].dis+1){p[v].dis=p[a.x].dis+1;node b;b.x=v;b.y=p[v].dis;q.push(b); }}}sort(p+1,p+1+n);printf("%d %d",p[1].num,p[1].dis);int same=p[1].dis,ans=1;for(int i=2;i<=n;i++){if(p[i].dis==same) ans++;else break;}printf(" %d",ans);return 0;
}

转载于:https://www.cnblogs.com/ruojisun/p/7354870.html

洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek相关推荐

  1. 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game

    洛谷 2953  [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...

  2. 2019.03.21【ZJOI2007】【BZOJ1095】【洛谷P2056】Hide 捉迷藏(DFS序)(线段树)

    BZOJ传送门 洛谷传送门 解析: 其实就是QTREE4的弱化版,可以直接用QTREE4的解法来做. 但是这道题有优秀的O(nlog⁡n)O(n\log n)O(nlogn)做法. 我们考虑利用DFS ...

  3. 洛谷UVA1328,POJ1961-Period【KMP,字符串】

    正题 洛谷评测记录:https://www.luogu.org/recordnew/lists?uid=52918&pid=UVA1328 POJ链接:http://poj.org/probl ...

  4. 洛谷:P2172 [国家集训队]部落战争

    题目链接: P2172 [国家集训队]部落战争 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 解题思路: 很明显的二分图:将原点与它能走到的点连一条边,然后做一遍最小点覆盖:即选出 ...

  5. 洛谷:P1129 [ZJOI2007] 矩阵游戏

    题目链接:P1129 [ZJOI2007] 矩阵游戏 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 解题思路: 首先不知道最大匹配数概念的可以看看这篇博客: (5条消息) 二分图及 ...

  6. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  7. 洛谷 P1142 轰炸

    洛谷 P1142 轰炸 题目描述 "我该怎么办?"飞行员klux向你求助. 事实上,klux面对的是一个很简单的问题,但是他实在太菜了. klux要想轰炸某个区域内的一些地方,它们 ...

  8. 洛谷 P1387 最大正方形

    P1387 最大正方形 题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=10 ...

  9. 洛谷P2763 试题库问题

    题目:https://www.luogu.org/problemnew/show/P2763 题目描述 «问题描述: 假设一个试题库中有n道试题.每道试题都标明了所属类别.同一道题可能有多个类别属性. ...

最新文章

  1. C#的简单不安全双向“混淆”
  2. 江苏省计算机一级考试知识点总结,江苏省计算机一级考试注意要点1
  3. 2018python培训-2018python深度学习核心技术培训班
  4. 小程序之实现滚动加载
  5. 2016,成为更好的自己
  6. python获取列表中元素的索引
  7. 计算数组的逆序对个数
  8. 多线程知识梳理(2) - 并发编程的艺术笔记
  9. Machine Learning(Stanford)| 斯坦福大学机(吴恩达)器学习笔记【汇总】
  10. 附录-系统环境、系统属性
  11. 由H264软编码可以看出,电脑的性能远远超过手机
  12. 上传漏洞-一句话木马
  13. PHP实现国密SM3算法
  14. HBuilder X如何运行uni-app 项目
  15. 大数据与云计算的理解与基本认识
  16. 计算机考试照片背景颜色,一寸照改背景颜色,一寸照片更换背景颜色
  17. 上海创蓝253董事长_从世界记忆大师到互联网百强企业CEO:创蓝253钛牛哥的传奇之路...
  18. 面试中java 知识点
  19. STM32F1单片机零基础学习(1)
  20. Ubantu20.04 安装搜狗输入法的详细步骤和遇到的问题

热门文章

  1. websocket 发送给前端一个对象_前端WebSocket封装
  2. 面试题如何实现一个IOC容器
  3. 不禁网页的浏览器_网页游戏兴衰史:「农场」没有菜,「渣渣辉」不贪玩
  4. 国开计算机应用基础中考答案,国开计算机应用基础模块4PowerPoint2010电子演示文稿系统答案...
  5. 后端:循环遍历的用法介绍
  6. SQL Server 数据库部分常用语句小结
  7. 全库模式 用户模式 表模式_暗模式,亮模式和用户的故事
  8. figma下载_我如何使用Figma,CSS Grid和CSS Flexbox构建登录页面
  9. figma下载_Figma中的动态内容和颜色
  10. 大三下学期十四周总结