zoukankan      html  css  js  c++  java
  • 洛谷 P2951 [USACO09OPEN]捉迷藏Hide and Seek

    题目描述

    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;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    javascript函数作用域及this指向详解
    使用div模拟textarea,实现文本输入框高度自适应(附:js控制textarea实现文本输入框高度自适应)
    css限制单行文本输入,超出部分使用...替换
    解决sea.js引用jQuery提示$ is not a function的问题
    js实用代码段(持续更新)
    xml中,button改变背景颜色方法
    Java中的import
    Unable to resolve target 'android-XX'的问题解决
    关于打开Eclipse时出现eclipse failed to create the java virtual machine与locking is not possible in the directory问题的解决
    Android SDK目录结构和工具介绍
  • 原文地址:https://www.cnblogs.com/ruojisun/p/7354870.html
Copyright © 2011-2022 走看看