直接最短路板子,dij堆优化。

题干:

题目描述贝茜在和约翰玩一个“捉迷藏”的游戏.她正要找出所有适合她躲藏的安全牛棚.一共有N(2≤N≤20000)个牛棚,被编为1到N号.她知道约翰(捉牛者)从牛棚1出发.所有的牛棚由M(1≤M≤50000)条双向路连接,每条双向路连接两个不同的牛棚.所有的牛棚都是相通的.贝茜认为同牛棚1距离最远的的牛棚是安全的.两个牛棚间的距离是指,从一个牛棚到另一个牛棚最少需要通过的道路数量.请帮贝茜找出所有的安全牛棚.
输入格式第1行输入两个整数N和M,之后M行每行输入两个整数,表示一条路的两个端点.输出格式仅一行,输出三个整数.第1个表示安全牛棚(如果有多个,输出编号最小的);第2个表示牛棚1和安全牛棚的距离;第3个表示有多少个安全的牛棚.样例输入
6 7
3 6
4 3
3 2
1 3
1 2
2 4
5 2
样例输出
4 2 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 + 7;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{char c;bool op = 0;while(c = getchar(), c < '0' || c > '9')if(c == '-') op = 1;x = c - '0';while(c = getchar(), c >= '0' && c <= '9')x = x * 10 + c - '0';if(op) x = -x;
}
template <class T>
void write(T x)
{if(x < 0) putchar('-'), x = -x;if(x >= 10) write(x / 10);putchar('0' + x % 10);
}
int dis[20005];
struct node
{int l,r,nxt;
}a[100005];
int len = 0,lst[20005];
priority_queue <pr,vector <pr>,greater<pr> > q;
bool vis[20005];
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,60,sizeof(dis));clean(vis);dis[1] = 0;q.push(mp(dis[1],1));while(!q.empty()){pr u = q.top();q.pop();int x = u.second;if(vis[x])continue;vis[x] = 1;for(int i = lst[x];i;i = a[i].nxt){int y = a[i].r;if(dis[y] > dis[x] + 1){dis[y] = dis[x] + 1;q.push(mp(dis[y],y));}}}
}
int n,m;
int main()
{read(n);read(m);duke(i,1,m){int x,y;read(x);read(y);add(x,y);add(y,x);}dij();int maxn = 0,cnt = 0,k = 0;duke(i,1,n){if(maxn < dis[i]){maxn = dis[i];cnt = 1;k = i;}else if(maxn == dis[i]){cnt++;}}
//    cout<<endl;printf("%d %d %d\n",k,dis[k],cnt);return 0;
}

转载于:https://www.cnblogs.com/DukeLv/p/9570077.html

B3402 [Usaco2009 Open]Hide and Seek 捉迷藏 最短路相关推荐

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

    题目描述 Bessie is playing hide and seek (a game in which a number of players hide and a single player ( ...

  2. BUUCTF [HCTF 2018] Hide and seek

    BUUCTF [HCTF 2018] Hide and seek 考点: 软连接读取任意文件 Flask伪造session /proc/self/environ文件获取当前进程的环境变量列表 rand ...

  3. BUUCTF:[HCTF 2018]Hide and seek

    BUUCTF:[HCTF 2018]Hide and seek 参考:https://www.jianshu.com/p/d20168da7284 先随便输入账号密码登录 提示我们上传zip文件 上传 ...

  4. BZOJ1941:[SDOI2010]Hide and Seek(K-D Tree)

    Description 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏- ...

  5. BZOJ 1941: [Sdoi2010]Hide and Seek KDtree + 估价函数

    Description 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏- ...

  6. bzoj1941 [Sdoi2010]Hide and Seek 线段树

    这个题其实应该算4个二维偏序,因为枚举每个点可以根据公式分成4个象限,4种计算方式,所以就考虑怎么求x和y的大小关系集合 首先,按x排序再枚举,可以保证后面的x大于前面的x,就相当于分成了两部分,剩下 ...

  7. hide and seek openai

    https://github.com/miyosuda/evolution_and_ai/tree/master/multi_agent_emergence My conda env: hide-an ...

  8. bzoj 3386 bzoj 3408: [Usaco2009 Oct]Heat Wave 热浪(最短路)

    3408: [Usaco2009 Oct]Heat Wave 热浪 Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 301  Solved: 223 [ ...

  9. 英语发音规则---H字母

    英语发音规则---H字母 一.总结 一句话总结: 1.H发[h]音? hot [hɒt] adj. 热的 house [haʊs] n. 住宅 head [hed] n. 头:头痛 hat [hæt] ...

最新文章

  1. 【青少年编程】绘制等腰直角三角形
  2. 超级有意思的代码注释
  3. Spring AOP里面的几个名词
  4. AndroidStudio导入httpmime jar编译不通过的解决办法
  5. RHEL6.2手动封装rpm源码包安装星际译王
  6. @Configuration和@Bean注解详解
  7. 复位BIOS解决电脑启动死机
  8. 【perl脚本】单行循环正则匹配
  9. 自己研发的系统给rtx发消息
  10. setw()函数使用
  11. Fabric CA的部署与使用
  12. 算法练习-Apples Prologue
  13. uefi启动适合什么计算机,uefi版和装机版有什么区别详解
  14. 一年中最后一个月的最后一天说说_2020只剩最后一个月的励志说说致自己
  15. 前端岗位一般的任职要求
  16. python随笔01(robotframework自动化)
  17. 2019年人工智能研发热点回眸
  18. ESP32 单片机学习笔记 - 06 - (以太网)Ethernet转Wifi
  19. win2003 php配置,Win2003 PHP环境配置
  20. 多渔:赚钱是场漫长的修行

热门文章

  1. 软件测试入职1年多薪资正常应该有多少?
  2. 78 bays朴素贝叶斯文本挖掘Chinese
  3. 《活着英文版自序》读后感
  4. pandas python2_python pandas二维数据分析
  5. Java基础——IO详解
  6. 首长,Redis 性能优化十三条军规立好了,请过目~
  7. 易福门接近开关IIM211
  8. 对偶量子计算机,广义量子干涉原理及对偶量子计算机.pdf
  9. ESIM:Enhanced LSTM for Natural Language Inference
  10. FIDO U2F HID协议