ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-05-20 22:54:26  阅读:264  来源: 互联网

标签:USACO09OPEN 洛谷 int P2951 number 距离 谷仓 hide barn


题目

 

题目描述

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了。

代码:

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define inf 233333333
 4 #define il inline
 5 #define ll long long
 6 il int gi()
 7 {
 8     int a=0;char x=getchar();bool f=0;
 9     while((x<'0'||x>'9')&&x!='-')x=getchar();
10     if(x=='-')x=getchar(),f=1;
11     while(x>='0'&&x<='9')a=a*10+x-48,x=getchar();
12     return f?-a:a;
13 }
14 int n,h[215000],dis[215000],cnt,m;
15 bool vis[105000];
16 struct edge{
17     int to,net,w;
18 }e[1000005];
19 il void add(int u,int v,int w)
20 {
21     e[++cnt].to=v;e[cnt].net=h[u];e[cnt].w=w;h[u]=cnt;
22 }
23 il void spfa()
24 {
25     queue<int>q;
26     for(int i=1;i<=n;i++)dis[i]=inf;
27     dis[1]=0;vis[1]=1;
28     q.push(1);
29     while(!q.empty())
30     {
31         int x=q.front();
32         vis[x]=0;q.pop();
33         for(int i=h[x];i;i=e[i].net)
34         if(dis[e[i].to]>dis[x]+e[i].w){
35          dis[e[i].to]=dis[x]+e[i].w;
36          if(!vis[e[i].to])q.push(e[i].to),vis[e[i].to]=1;
37         }
38         }
39 }
40 int main()
41 {
42     n=gi(),m=gi();
43     int u,v,w;
44     for(int i=1;i<=m;i++){
45         u=gi(),v=gi();
46         add(u,v,1),add(v,u,1);
47     }
48     spfa();
49     int ans,tot=0,dist=0;
50     for(int i=1;i<=n;i++)dist=max(dist,dis[i]);
51     for(int i=1;i<=n;i++)if(dist==dis[i]){ans=i;break;}
52     for(int i=1;i<=n;i++)if(dis[i]==dist)tot++;
53     printf("%d %d %d",ans,dist,tot);
54     return 0;
55 }

 

标签:USACO09OPEN,洛谷,int,P2951,number,距离,谷仓,hide,barn
来源: https://blog.51cto.com/u_15180869/2799035

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有