B3402 [Usaco2009 Open]Hide and Seek 捉迷藏 最短路
直接最短路板子,dij堆优化。
题干:
题目描述
贝茜在和约翰玩一个“捉迷藏”的游戏.
她正要找出所有适合她躲藏的安全牛棚.一共有N(≤N≤)个牛棚,被编为1到N号.她知道约翰(捉牛者)从牛棚1出发.所有的牛棚由M(≤M≤)条双向路连接,每条双向路连接两个不同的牛棚.所有的牛棚都是相通的.贝茜认为同牛棚1距离最远的的牛棚是安全的.两个牛棚间的距离是指,从一个牛棚到另一个牛棚最少需要通过的道路数量.请帮贝茜找出所有的安全牛棚.
输入格式
第1行输入两个整数N和M,之后M行每行输入两个整数,表示一条路的两个端点. 输出格式
仅一行,输出三个整数.第1个表示安全牛棚(如果有多个,输出编号最小的);第2个表示牛棚1和安全牛棚的距离;第3个表示有多少个安全的牛棚. 样例输入 样例输出 提示
没有写明提示
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
#define mp make_pair
#define pr pair<int,int>
const int INF = 1e9 + ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
int dis[];
struct node
{
int l,r,nxt;
}a[];
int len = ,lst[];
priority_queue <pr,vector <pr>,greater<pr> > q;
bool vis[];
void add(int x,int y)
{
a[++len].l = x;
a[len].r = y;
a[len].nxt = lst[x];
lst[x] = len;
}
void dij()
{
memset(dis,,sizeof(dis));
clean(vis);
dis[] = ;
q.push(mp(dis[],));
while(!q.empty())
{
pr u = q.top();
q.pop();
int x = u.second;
if(vis[x])
continue;
vis[x] = ;
for(int i = lst[x];i;i = a[i].nxt)
{
int y = a[i].r;
if(dis[y] > dis[x] + )
{
dis[y] = dis[x] + ;
q.push(mp(dis[y],y));
}
}
}
}
int n,m;
int main()
{
read(n);read(m);
duke(i,,m)
{
int x,y;
read(x);read(y);
add(x,y);
add(y,x);
}
dij();
int maxn = ,cnt = ,k = ;
duke(i,,n)
{
if(maxn < dis[i])
{
maxn = dis[i];
cnt = ;
k = i;
}
else if(maxn == dis[i])
{
cnt++;
}
}
// cout<<endl;
printf("%d %d %d\n",k,dis[k],cnt);
return ;
}
B3402 [Usaco2009 Open]Hide and Seek 捉迷藏 最短路的更多相关文章
- BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏(最短路)
这个= =一看就是最短路了= = PS:最近有点懒 = = 刚才看到一道平衡树的裸题还嫌懒不去写= =算了等刷完这堆水题再去理= = CODE: #include<cstdio>#incl ...
- 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】3402: [Usaco2009 Open]Hide and Seek 捉迷藏(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=3402 又是spfa水题.. #include <cstdio> #include < ...
- BZOJ—— 3402: [Usaco2009 Open]Hide and Seek 捉迷藏
http://www.lydsy.com/JudgeOnline/problem.php?id=3402 Description 贝茜在和约翰玩一个“捉迷藏”的游戏. 她正要找出所有适 ...
- 洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek
题目戳 题目描述 Bessie is playing hide and seek (a game in which a number of players hide and a single play ...
- Luogu 2951 捉迷藏Hide and Seek
P2951 [USACO09OPEN]捉迷藏Hide and Seek 题目描述 Bessie is playing hide and seek (a game in which a number o ...
- 【BZOJ-1941】Hide and Seek KD-Tree
1941: [Sdoi2010]Hide and Seek Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 830 Solved: 455[Submi ...
随机推荐
- Java jre7及以上版本中的switch支持String的实现细节
Java7中的switch支持String的实现细节 作者: zsxwing 更新: 2013-03-04 21:08:02 发布: 2012-04-26 13:58:19 在Java7之前,swit ...
- html5——边框
精确控制 /*水平半径 垂直半径;*/ border-top-left-radius: 30px 40px; border-top-right-radius: 30px 40px; border-bo ...
- Centos6.7 安装Naigos教程
Centos6.7 安装Naigos教程参考文档:https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/quickst ...
- (转)分布式文件存储FastDFS(四)配置fastdfs-apache-module
http://blog.csdn.net/xingjiarong/article/details/50560605 在前边我们已经配置好了FastDFS的环境,但是此时的FastDFS还不能通过htt ...
- Linux添加FTP用户并设置权限
在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建用户,命令行状态下,在root用户下: 运 ...
- 20190625_mysql5.7查看及其解锁_被锁的表
[root@localhost ~]# mysql -u myroot -pEnter password: mysql> show OPEN TABLES where In_use > 0 ...
- MVC Ajax调用Action时-OnActionExecuting RedirectResult 无法跳转的处理办法
public class BaseController : Controller { protected override void OnActionExecuting(ActionExecuting ...
- Oracle数据库的自动备份脚本
@echo off echo ================================================ echo Windows环境下Oracle数据库的自动备份脚本 echo ...
- 【JAVA】简陋的学生信息管理系统
因为之前写了一个学生信息管理系统,但还是处于命令行界面,不美观,于是打算做一个完整的界面出来. 在网上查阅资料后发现C++本身是不支持图形化界面的(可以使用第三方的Qt来做) 权衡之下还是选择了JAV ...
- (ccf)201709-4 通信网络
#include<iostream> #include<memory.h> #include<stack> #include<string> #incl ...