USACO Hide and Seek
洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek
JDOJ 2641: USACO 2009 Open Silver 1.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.
仅一行,三个整数,两两中间空格隔开。表示:距离第一个谷仓最远的谷仓编号(如有多个则输出编号最小的。),以及最远的距离,和有几个谷仓距离第一个谷仓那么远。
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的更多相关文章
- 【BZOJ-1941】Hide and Seek KD-Tree
1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 830 Solved: 455[Submi ...
- [BZOJ1941][Sdoi2010]Hide and Seek
[BZOJ1941][Sdoi2010]Hide and Seek 试题描述 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他 ...
- BZOJ3402: [Usaco2009 Open]Hide and Seek 捉迷藏
3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 51 Solved: 4 ...
- BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏
题目 3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec Memory Limit: 128 MB Description 贝 ...
- 3402: [Usaco2009 Open]Hide and Seek 捉迷藏
3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 78 Solved: 6 ...
- bzoj:1941: [Sdoi2010]Hide and Seek
1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 531 Solved: 295[Submi ...
- 【BZOJ】【1941】【SDOI2010】Hide and Seek
KD-Tree 一开始看错题了
- 洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek
题目戳 题目描述 Bessie is playing hide and seek (a game in which a number of players hide and a single play ...
- 【BZOJ1941】Hide and Seek(KD-Tree)
[BZOJ1941]Hide and Seek(KD-Tree) 题面 BZOJ 洛谷 题解 \(KD-Tree\)对于每个点搜一下最近点和最远点就好了 #include<iostream> ...
随机推荐
- [原创]App崩溃率统计工具推荐
[原创]App崩溃率统计工具推荐 1 友盟(推荐) 友盟是一款比较成熟的工具,同时也可以展示留存,日活,事件等. 2 Bugly 腾讯的bugly统计数据也算是比较早的,可惜后续维护比较弱,功能与 ...
- win10如何将wps设置成默认应用
1.在此之前,我们当然需要下载一个WPS软件了.如果还没有安装软件的,大家可以去网上搜一下“WPS”进入官网下载; 2.下载之后,我们进入开始菜单,然后点击所有应用,找到WPS; 3.之后就会看见“配 ...
- mybatis 的 dao 接口跟 xml 文件里面的 sql 是如何建立关系的?
mybatis 会先解析这些xml 文件,通过 xml 文件里面的命名空间 (namespace)跟dao 建立关系:然后 xml 中的每段 sql 会有一个id 跟 dao 中的接口进行关联. 那么 ...
- 【开源监控】Grafana介绍与安装
Grafana介绍与安装 Grafana介绍 场景:由于业务场景,有多个组织机构.需要在某个组织结构下,完成对本机构下的系统的实时监控以及可视化展示.底层已经用zabbix对监控指标做了数据的采集. ...
- 单点登录(sso)入门
单点登录的英文名叫做Single Sign On,简称SSO. 在以前,一般我们就单系统,所有的功能都在同一个系统上. 后来,我们为了合理利用资源和降低耦合性,于是把单系统拆分成多个子系统. 比如阿里 ...
- 《 .NET并发编程实战》阅读指南 - 第6章
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.
- 【C#常用方法】2.DataTable(或DataSet)与Excel文件之间的导出与导入(使用NPOI)
DataTable与Excel之间的互导 1.项目添加NPOI的引用 NPOI项目简介: NPOI是一个开源的C#读写Excel.WORD等微软OLE2组件文档的项目,特点是可以在没有安装Office ...
- vs2017 项目生成时不产生xml文件的方法
在项目.csproj文件 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' & ...
- Layer.js弹层的一些简单的使用
//-----------这里只是简单的做一下记录,没有封装,作为笔记防止忘记了 //----contentMsg 里面是可以传入 HTML代码的 top.layer.alert(contentMsg ...
- 并发编程需要场景化:CountDownLatch