P2951 [USACO09OPEN]捉迷藏Hide and Seek

题目描述

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 的贡献翻译


跑一遍最短路啊,easy,SPFA水过,啦啦啦啦啦

代码君

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 1000008
#define INF 2147483647 using namespace std; int first[MAXN], next[MAXN];
int u[MAXN], v[MAXN], w[MAXN];
int dis[MAXN];
bool vis[MAXN];
int n, m, s;
queue<int> P; int main() {
memset(first, -1, sizeof(first));
s = 1;
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++) {
dis[i] = INF;
}
dis[s] = 0;
for(int i=1; i<=2*m; i++) {
scanf("%d%d", &u[i], &v[i]);
w[i] = 1;
next[i] = first[u[i]];
first[u[i]] = i;
i++;
u[i] = v[i-1], v[i] = u[i-1], w[i] = 1;
next[i] = first[u[i]];
first[u[i]] = i;
}
P.push(s), vis[s] = 1;
while(!P.empty()) {
int x = P.front();
int k = first[x];
P.pop();
while(k != -1) {
if(dis[v[k]] > dis[u[k]]+w[k]) {
dis[v[k]] = dis[u[k]]+w[k];
if(vis[v[k]] == 0) {
P.push(v[k]);
vis[v[k]] = 1;
}
}
k = next[k];
}
vis[x] = 0;
}
int maxn = -1, ans = 1, ANS;
for(int i=1; i<=n; i++) {
if(dis[i] == maxn) ans++;
if(dis[i] > maxn&&dis[i] != MAXN) {maxn = dis[i]; ans = 1; ANS = i;}
}
printf("%d %d %d", ANS, maxn, ans);
return 0;
}

  

Luogu 2951 捉迷藏Hide and Seek的更多相关文章

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

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

  2. [USACO09OPEN]捉迷藏Hide and Seek

    OJ题号:洛谷2951 思路:Dijkstra+堆优化.注意是无向图,所以加边时要正反各加一遍. #include<cstdio> #include<vector> #incl ...

  3. P2951 【[USACO09OPEN]捉迷藏Hide and Seek】

    典型的最短路,而且只要再加一点点操作,就能得到答案 所以可以直接套模板 具体看程序:: #include<cstdio> #include<queue>//队列专属头文件 #i ...

  4. USACO Hide and Seek

    洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek 洛谷传送门 JDOJ 2641: USACO 2009 Open Silver 1.Hide and Seek JDOJ传 ...

  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. BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

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

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

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

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

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

  9. [BZOJ1941][Sdoi2010]Hide and Seek

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

随机推荐

  1. com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$Mis

    为什么会出现这个错误? 我使用了Maven配置Struts,所以改动Struts配置文件的默认路径,例如以下: <!-- 配置struts --> <filter> <f ...

  2. Python核心编程学习笔记(一)

    1.把一个字符串赋值给变量str.先用print来显示变量的内容,然后用变量名称来显示: >>>str = 'Hello World!' >>>print str ...

  3. EF:插入记录失败,提示当 IDENTITY_INSERT 设置为 OFF 时,不能向表中的标识列插入显式值

    EF忽然报错:增加记录时,提示当 IDENTITY_INSERT 设置为 OFF 时,不能向表 '...' 中的标识列插入显式值.真奇怪,添加记录方法一向好地地,从没出过错.要出错,那也是Update ...

  4. new Modifier (C# Reference)

    https://msdn.microsoft.com/en-us/library/435f1dw2.aspx When used as a declaration modifier, the new  ...

  5. go语言笔记——调试还很弱,用gdb来做?可用panic和defer。格式化代码使用gofmt,貌似我的vim插件是自带

    3.3 调试器 应用程序的开发过程中调试是必不可少的一个环节,因此有一个好的调试器是非常重要的,可惜的是,Go 在这方面的发展还不是很完善.目前可用的调试器是 gdb,最新版均以内置在集成开发环境 L ...

  6. P3202 [HNOI2009]通往城堡之路 神仙题

    这个题不是坑人吗...写个tarjan标签,然后拿这么个神仙题来搞...代码有点看不懂,有兴趣的可以去洛谷题解区看看,懒得想了. 题干: 题目描述 听说公主被关押在城堡里,彭大侠下定决心:不管一路上有 ...

  7. bzoj2780

    AC自动机+树链剖分+线段树/树状数组+dfs序+树链的并 题意:给出n个母串和q个询问串,对于每个询问串输出有多少个母串包含这个询问串 N=∑|母串|<=10^5 Q=∑|询问串|<=3 ...

  8. docker部署gitlab服务

    docker run -d -p : -p : -p : \ --name gitlab --restart always \ --volume /home/xxx/dockerData/gitlab ...

  9. 【WIP】Bootstrap nav

    创建: 2017/09/28   更新: 2017/10/14 标题加上[WIP]

  10. [Swift通天遁地]七、数据与安全-(12)使用Instruments Leaks工具检测内存泄露

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...