【BZOJ】3402: [Usaco2009 Open]Hide and Seek 捉迷藏(spfa)

http://www.lydsy.com/JudgeOnline/problem.php?id=3402

又是spfa水题。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
  
const int N=20005, Q=N*10, oo=~0u>>2;
int q[Q], front, tail, d[N], vis[N], ihead[N], cnt, n, m;
struct ED { int to, next; }e[Q];
void add(int u, int v) {
    e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
    e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;
}
void spfa() {
    q[tail++]=1;
    for1(i, 2, n) d[i]=oo;
    vis[1]=1;
    while(front!=tail) {
        int v, u=q[front++]; if(front==Q) front=0; vis[u]=0;
        for(int i=ihead[u]; i; i=e[i].next) if(d[v=e[i].to]>d[u]+1) {
            d[v]=d[u]+1;
            if(!vis[v]) {
                vis[v]=1;
                q[tail++]=v; if(tail==Q) tail=0;
            }
        }
    }
}
  
int main() {
    read(n); read(m);
    for1(i, 1, m) {
        int u=getint(), v=getint();
        add(u, v);
    }
    spfa();
    int mx=0, ans=0, id=oo;
    for1(i, 1, n) mx=max(d[i], mx);
    for1(i, 1, n) if(d[i]==mx) { ++ans; id=min(i, id); }
    printf("%d %d %d", id, mx, ans);
    return 0;
}

 

 


 

 

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

Source

posted @ 2014-09-14 17:56  iwtwiioi  阅读(185)  评论(0编辑  收藏  举报