洛谷P2951 [USACO09OPEN]捉迷藏Hide and Seek
来源:互联网 发布:阿里云鹰眼 编辑:程序博客网 时间:2024/06/04 17:47
题目描述
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.
仅一行,三个整数,两两中间空格隔开。表示:距离第一个谷仓最远的谷仓编号(如有多个则输出编号最小的。),以及最远的距离,和有几个谷仓距离第一个谷仓那么远。
输入输出样例
6 7 3 6 4 3 3 2 1 3 1 2 2 4 5 2
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。
最短路问题。。。
注意,题目有误,数组应该开到 100000 以上。。。
附代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>#define MAX 99999999#define MAXM 100010using namespace std;int n,m,c=1,head[MAXM],path[MAXM];bool vis[MAXM];struct node{int next,to,w;}a[MAXM];inline int read(){int date=0,w=1;char c=0;while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}return date*w;}inline int relax(int u,int v,int w){if(path[v]>path[u]+w){path[v]=path[u]+w;return 1;}return 0;}void add(int u,int v,int w){a[c].to=v;a[c].w=w;a[c].next=head[u];head[u]=c++;a[c].to=u;a[c].w=w;a[c].next=head[v];head[v]=c++;}void spfa(){int u,v;queue<int> q;for(int i=1;i<=n;i++){path[i]=MAX;vis[i]=false;}q.push(1);path[1]=0;vis[1]=true;while(!q.empty()){u=q.front();q.pop();vis[u]=false;for(int i=head[u];i;i=a[i].next){v=a[i].to;if(relax(u,v,a[i].w)&&!vis[v]){vis[v]=true;q.push(v);}}}int maxn=0,s,ans=0;for(int i=2;i<=n;i++)if(path[i]>maxn&&path[i]<MAX){maxn=path[i];s=i;}printf("%d %d ",s,maxn);for(int i=2;i<=n;i++)if(path[i]==maxn&&path[i]<MAX)ans++;printf("%d\n",ans);}int main(){int x,y;n=read();m=read();for(int i=1;i<=m;i++){x=read();y=read();add(x,y,1);}spfa();return 0;}
- 洛谷P2951 [USACO09OPEN]捉迷藏Hide and Seek
- bzoj3402 [Usaco2009 Open]Hide and Seek 捉迷藏
- BZOJ3402: [Usaco2009 Open]Hide and Seek 捉迷藏
- bzoj3402 [Usaco2009 Open]Hide and Seek 捉迷藏
- 3402: [Usaco2009 Open]Hide and Seek 捉迷藏 最短路
- BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏 最短路
- bzoj 3402: [Usaco2009 Open]Hide and Seek 捉迷藏
- SPFA——热浪Heat Wave、捉迷藏Hide and Seek、电车
- 躲猫猫 hide-and-seek
- HYSBZ1941-Hide and Seek
- bzoj-1941 Hide and Seek
- [BZOJ 1941]Hide and Seek
- [BZOJ1941][Sdoi2010]Hide and Seek
- [bzoj1941][SDOI2010]Hide and Seek
- 【SDOI2010】【BZOJ1941】Hide and Seek
- bzoj1941: [Sdoi2010]Hide and Seek
- 1941: [Sdoi2010]Hide and Seek
- 【SDOI2010】bzoj1941 Hide and Seek
- NFS 客户端机运行"df"命令后死掉(没有反应) -- 解决
- linux网桥处理函数学习------br_handle_frame_finish
- 利用js的call函数,实现this的改变
- Redis中删除过期Key的三种策略
- canal
- 洛谷P2951 [USACO09OPEN]捉迷藏Hide and Seek
- Oracle存储过程学习
- Zookeeper JAVA客户端(Kotlin 实现) CRUD
- Handlebars模板引擎demo
- Matlab trapz函数和cumtrapz函数
- npm 安装包报错 rollbackFailedOptional
- 一个关于php将资源提供进行下载的操作类
- Centos7+django1.11+ngnix+uwsgi+python3.6项目部署
- (1)VS2017 Xamarin.Android 部署