zoukankan      html  css  js  c++  java
  • 【BZOJ】3402: [Usaco2009 Open]Hide and Seek 捉迷藏 最短路

    Description

        贝茜在和约翰玩一个“捉迷藏”的游戏.
        她 正要找出所有适合她躲藏的安全牛棚.一共有 N(2≤N≤20000)个牛棚,被编为1到N号.她知道约翰(捉牛者)从牛棚1出发.所有的牛棚由M(1≤M≤50000)条双向路连接,每条双向路连 接两个不同的牛棚.所有的牛棚都是相通的.贝茜认为同牛棚1距离最远的的牛棚是安全的.两个牛棚间的距离是指,从一个牛棚到另一个牛棚最少需要通过的道路 数量.请帮贝茜找出所有的安全牛棚.

    Input

        第1行输入两个整数N和M,之后M行每行输入两个整数,表示一条路的两个端点.
       

    Output

     仅一行,输出三个整数.第1个表示安全牛棚(如果有多个,输出编号最小的);第2个表示牛棚1和安全牛棚的距离;第3个表示有多少个安全的牛棚.

    Sample Input

    6 7
    3 6
    4 3
    3 2
    1 3
    1 2
    2 4
    5 2

    Sample Output

    4 2 3

    HINT

      边权为1的SPFA。。。我居然没有写挂真是令人激动啊。。。

     

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 using namespace std;
     7 const int MAXN=20001;
     8 const int MAXM=100001;
     9 int head[MAXM],dist[MAXN],n,m,cnt;
    10 bool visit[MAXN];
    11 queue <int> Q;
    12 struct xx
    13 {
    14     int to,nxt,w;
    15 }e[MAXM];
    16 void add(int x,int y)
    17 {
    18     cnt++;
    19     e[cnt].to=y;
    20     e[cnt].nxt=head[x];
    21     e[cnt].w=1;
    22     head[x]=cnt;
    23 }
    24 void SPFA()
    25 {
    26     int i,t;
    27     memset(dist,0x7f,sizeof(dist));
    28     memset(visit,0,sizeof(visit));
    29     Q.push(1);
    30     visit[1]=1;
    31     dist[1]=0;
    32     while(!Q.empty())
    33     {
    34         t=Q.front();
    35         Q.pop();
    36         visit[t]=0;
    37         for(i=head[t];i;i=e[i].nxt)
    38         {
    39             if(dist[e[i].to]>dist[t]+e[i].w)
    40             {
    41                 dist[e[i].to]=dist[t]+e[i].w;
    42                 if(!visit[e[i].to]) visit[e[i].to]=1,Q.push(e[i].to);
    43             }
    44         }
    45     }
    46 }
    47 int main(int argc, char *argv[])
    48 {
    49     int i,j=0,x,y,d=0,ans;
    50     scanf("%d%d",&n,&m);
    51     for(i=1;i<=m;i++)
    52     scanf("%d%d",&x,&y),add(x,y),add(y,x);
    53     SPFA();
    54     for(i=1;i<=n;i++)
    55     {
    56         if(dist[i]>j) j=dist[i],ans=i;
    57     }
    58     for(i=1;i<=n;i++)
    59     if(dist[i]==j) d++;
    60     printf("%d %d %d
    ",ans,j,d);
    61     return 0;
    62 }
  • 相关阅读:
    进程与线程
    the art of seo(chapter seven)
    the art of seo(chapter six)
    the art of seo(chapter five)
    the art of seo(chapter four)
    the art of seo(chapter three)
    the art of seo(chapter two)
    the art of seo(chapter one)
    Sentinel Cluster流程分析
    Sentinel Core流程分析
  • 原文地址:https://www.cnblogs.com/BeyondW/p/5832080.html
Copyright © 2011-2022 走看看