1. 程式人生 > >洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek

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

out which multiple blank 但是 -s farm 最大的 www.

題目描述

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 A_i and B_i (1 <= A_i <= N; 1 <= B_i <= N; A_i != B_i); 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.

奶牛貝西和農夫約翰(FJ)玩捉迷藏,現在有N個谷倉,FJ開始在第一個谷倉,貝西為了不讓FJ找到她,當然要藏在距離第一個谷倉最遠的那個谷倉了。現在告訴你N個谷倉,和M個兩兩谷倉間的“無向邊”。每兩個倉谷間當然會有最短路徑,現在要求距離第一個谷倉(FJ那裏)最遠的谷倉是哪個(所謂最遠就是距離第一個谷倉最大的最短路徑)?如有多個則輸出編號最小的。以及求這最遠距離是多少,和有幾個這樣的谷倉距離第一個谷倉那麽遠。

輸入輸出格式

輸入格式:

  • Line 1: Two space-separated integers: N and M

  • Lines 2..M+1: Line i+1 contains the endpoints for path i: A_i and B_i

第一行:兩個整數N,M;

第2-M+1行:每行兩個整數,表示端點A_i 和 B_i 間有一條無向邊。

輸出格式:

  • 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 
輸出樣例#1:
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.

這裏谷倉4,5,6距離1號谷倉都是2,但是4編號最小所以輸出4.因此最遠距離是2且有3個谷倉,依次輸出:2和3。

感謝 wjcwinmt 的貢獻翻譯

最短路

堆優化dijkstra練習

屠龍寶刀點擊就送

#include <algorithm>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <queue>
#define N 100005
using namespace std;
struct node
{
    int x,y;
    bool operator<(node a)const
    {
        return y>a.y;
    }
};
struct dist
{
    int num,dis;
    bool operator<(dist b)const
    {
        if(dis==b.dis) return num<b.num;
        return dis>b.dis;
    }
}p[N];
priority_queue<node>q;
inline void read(int &x)
{
    x=0;bool f=0;
    register char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch==-) f=1;
    for(; isdigit(ch);ch=getchar()) x=x*10+ch-0;
    x=f?-x:x; 
}
bool vis[N];
int n,m,head[N],to[N],Next[N],cnt;
int main()
{
    freopen("hideseek.in","r",stdin);freopen("hideseek.out","w",stdout);
    read(n);
    read(m);
    for(int x,y;m--;)
    {
        read(x);
        read(y);
        Next[++cnt]=head[x];to[cnt]=y;head[x]=cnt;
        Next[++cnt]=head[y];to[cnt]=x;head[y]=cnt;
    }
    for(int i=1;i<=n;i++) p[i].num=i,p[i].dis=0x7ffffff;
    p[1].dis=0;
    node a;
    a.x=1;a.y=p[1].dis;
    q.push(a);
    while(!q.empty())
    {
        node a=q.top();q.pop();
        if(vis[a.x]) continue;
        vis[a.x]=1;
        for(int i=head[a.x];i;i=Next[i])
        {
            int v=to[i];
            if(p[v].dis>p[a.x].dis+1)
            {
                p[v].dis=p[a.x].dis+1;
                node b;
                b.x=v;b.y=p[v].dis;
                q.push(b); 
            }
        }
    }
    sort(p+1,p+1+n);
    printf("%d %d",p[1].num,p[1].dis);
    int same=p[1].dis,ans=1;
    for(int i=2;i<=n;i++)
    {
        if(p[i].dis==same) ans++;
        else break;
    }
    printf(" %d",ans);
    return 0;
}

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