题目

题目描述

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。

Solution:

炒鸡水题,做法就是直接跑spfa求最短路,然后对于dis数组处理一下就OK了。

代码:

 #include<bits/stdc++.h>
using namespace std;
#define inf 233333333
#define il inline
#define ll long long
il int gi()
{
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=a*+x-,x=getchar();
return f?-a:a;
}
int n,h[],dis[],cnt,m;
bool vis[];
struct edge{
int to,net,w;
}e[];
il void add(int u,int v,int w)
{
e[++cnt].to=v;e[cnt].net=h[u];e[cnt].w=w;h[u]=cnt;
}
il void spfa()
{
queue<int>q;
for(int i=;i<=n;i++)dis[i]=inf;
dis[]=;vis[]=;
q.push();
while(!q.empty())
{
int x=q.front();
vis[x]=;q.pop();
for(int i=h[x];i;i=e[i].net)
if(dis[e[i].to]>dis[x]+e[i].w){
dis[e[i].to]=dis[x]+e[i].w;
if(!vis[e[i].to])q.push(e[i].to),vis[e[i].to]=;
}
}
}
int main()
{
n=gi(),m=gi();
int u,v,w;
for(int i=;i<=m;i++){
u=gi(),v=gi();
add(u,v,),add(v,u,);
}
spfa();
int ans,tot=,dist=;
for(int i=;i<=n;i++)dist=max(dist,dis[i]);
for(int i=;i<=n;i++)if(dist==dis[i]){ans=i;break;}
for(int i=;i<=n;i++)if(dis[i]==dist)tot++;
printf("%d %d %d",ans,dist,tot);
return ;
}

洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek的更多相关文章

  1. [USACO09OPEN]捉迷藏Hide and Seek

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

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

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

  3. Luogu 2951 捉迷藏Hide and Seek

    P2951 [USACO09OPEN]捉迷藏Hide and Seek 题目描述 Bessie is playing hide and seek (a game in which a number o ...

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

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

  5. 洛谷 P2056 [ZJOI2007]捉迷藏 || bzoj 1095: [ZJOI2007]Hide 捉迷藏 || 洛谷 P4115 Qtree4 || SP2666 QTREE4 - Query on a tree IV

    意识到一点:在进行点分治时,每一个点都会作为某一级重心出现,且任意一点只作为重心恰好一次.因此原树上任意一个节点都会出现在点分树上,且是恰好一次 https://www.cnblogs.com/zzq ...

  6. 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling 题解

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...

  7. 洛谷 P2056 [ZJOI2007]捉迷藏 解题报告

    P2056 [ZJOI2007]捉迷藏 题目描述 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由\ ...

  8. 洛谷 P2056 [ZJOI2007]捉迷藏 题解【点分治】【堆】【图论】

    动态点分治入 门 题? 题目描述 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由 \(N\) 个屋 ...

  9. 洛谷——P2952 [USACO09OPEN]牛线Cow Line

    P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a l ...

随机推荐

  1. 【Todo】找出共同好友 & Spark & Hadoop面试题

    找了这篇文章看了一下面试题<Spark 和hadoop的一些面试题(准备)> http://blog.csdn.net/qiezikuaichuan/article/details/515 ...

  2. C#:在AnyCPU模式下使用CefSharp

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述如何在AnyCPU模式下使用CefSharp 因为在某些情况下,不得不用AnyCPU,但是CefS ...

  3. idea 新建 maven项目遇到的一些问题

    idea创建好了maven项目之后,需要先在项目中添加 Web,这里创建Web时就会要求fix一个Artifacts,新建即可,然后面板设置默认即可(shift+ctrl+alt+s 打开面板): 然 ...

  4. html5新特性localStorage和sessionStorage

    HTML5 提供了两种在客户端存储数据的新方法: localStorage: (1)它的生命周期是永久的,关闭页面或浏览器之后localStorage中的数据也不会消失. (2)它的容量大小是5M作用 ...

  5. spring 属性文件加载接口---PropertySourceLoader

    org.springframework.boot.config Interface PropertySourceLoader 实现类: PropertiesPropertySourceLoader, ...

  6. 使用MyBatis遇到的一些需要记录下的问题

    (1)MyBaits结果集返回Map,Map集合乱序. xml 中的SQL 输出: 改成: 输出: 目测跟字母顺序有关:ABCDEFGHIJKLMNOPQRSTUVWXYZ (2)需要对字段动态排序 ...

  7. Could not resolve placeholder 'jdbc.url' in value "${jdbc.url}"

    写完接口之后,发现报了这个错误,查了一下发现,spring不允许使用两个 <context:property-placeholder>

  8. 详解Python中的下划线

    本文将讨论Python中下划线(_)字符的使用方法.我们将会看到,正如Python中的很多事情,下划线的不同用法大多数(并非所有)只是常用惯例而已. 单下划线(_) 通常情况下,会在以下3种场景中使用 ...

  9. Scrum立会报告+燃尽图(十一月十五日总第二十三次):代码规范与技术交流

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2384 项目地址:https://git.coding.net/zhang ...

  10. Java 学习笔记 ------第五章 对象封装

    本章学习目标: 了解封装的概念与实现 定义类.构造函数与方法 使用方法重载与不定长度自变量 了解static方法 一.Java封装概念 在面向对象程式设计方法中,封装(英语:Encapsulation ...