牛客假日团队赛10 H——Hide and Seek(spfa+双向队列)

链接:https://ac.nowcoder.com/acm/contest/1072/H
来源:牛客网

题目描述 :
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 Ai and Bi (1 <= Ai <= N; 1 <= Bi <= N; Ai != =Bi); 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.
输入描述:

  • Line 1: Two space-separated integers: N and M
  • Lines 2…M+1: Line i+1 contains the endpoints for path i: Ai and Bi
    输出描述:
  • 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
    输出:
    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.
    题意思路:懒了,不想翻译了。题意大概就是:给你n个点,m条路,相邻点之间的距离是1,让你求:距离源点1最远的点xi的序号,到点1的距离,有几个与xi到点1同样距离的点。
    代码:
#include <iostream>
#include <deque>
#include <algorithm>

#define SNUM int(2e4 + 9)
#define IMAX 0x3f3f3f3f

using namespace std;

int pn, wn, f[SNUM];
deque<int> m[SNUM];

void spfa()
{
	deque<int> w;
	w.push_back(1);
	f[1] = 0;
	while (!w.empty())
	{
		int t = w.front();
		w.pop_front();
		deque<int>::iterator p;
		for (p = m[t].begin(); p != m[t].end(); p++)
		{
			if (f[*p] == IMAX)
			{
				w.push_back(*p);
				f[*p] = min(f[*p], f[t] + 1);
			}
		}
	}
	return ;
}

int main()
{
	/*ios::sync_with_stdio(0);
	cin.tie(0);*/使cin和cout的效率接近于scanf和printf
	std::ios::sync_with_stdio(false);
    std::cin.tie(0);
	cin >> pn >> wn;
	for (int i = 1; i <= pn; i++) f[i] = IMAX;
	for (int i = 1; i <= wn; i++)
	{
		int a, b;
		cin >> a >> b;
		m[a].push_back(b);//在a后插入b,a后是b
		m[b].push_back(a);//在b后插入a,b后是a
	}
	spfa();
	int a = 1, c = 0, s = 1;//s最小的序号,a距离,c有几个相同的
	for (int i = 2; i <= pn; i++)
	{
		//cout << f[i] << ' ';
		if (f[i] == a) c += 1;
		if (f[i] > a && f[i] < IMAX) c = 1, a = f[i], s = i;
	}
	cout << s << ' ' << a << ' ' << c << endl;
	return 0;
}

/*在ACM里,经常出现 数据集超大造成 cin TLE的情况。这时候大部分人(包括原来我也是)认为
这是cin的效率不及scanf的错,甚至还上升到C语言和C++语言的执行效率层面的无聊争论。其 实像上文所说,
这只是C++为了兼容而采取的保守措施。我们可以在IO之前将stdio解除绑定,这样做了之后要注意不要同时
混用cout和printf 之类。
在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。
可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。*/


感悟:真是很多题都能用到数据结构,此题结合了spfa。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值