• POJ 2253


    Time Limit: 1000MS Memory Limit: 65536K

    Description

    Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping. 
    Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps. 
    To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence. 
    The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones. 

    You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone. 

    Input

    The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.

    Output

    For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

    Sample Input

    2
    0 0
    3 4
    
    3
    17 4
    19 4
    18 5
    
    0
    

    Sample Output

    Scenario #1
    Frog Distance = 5.000
    
    Scenario #2
    Frog Distance = 1.414

    题解:

    参考:http://www.cnblogs.com/tanhehe/p/3169865.html

    另外请参考:http://blog.csdn.net/PKU_ZZY/article/details/52434239

    Dijkstra算法模板:

    const int INF=0x3f3f3f3f;
    const int maxn=1200;
    
    int n; //n个节点
    int d[maxn],edge[maxn][maxn];
    bool vis[maxn]; //标记是否在集合S中
    
    /*
    集合V:图上所有节点
    集合S:已经确定正确计算出d[]的节点
    集合Q:V-S
    */
    void dijkstra()
    {
        for(int i=1;i<=n;i++) d[i]=(i==1)?0:INF;
        memset(vis,0,sizeof(vis));
    
        for(int i=1;i<=n;i++)
        {
            int mini=INF,u;
            for(int j=1;j<=n;j++)
            {
                if(!vis[j] && d[j]<mini) mini=d[(u=j)]; //寻找集合Q里d[u]最小的那个点u
            }
            vis[u]=1; //放入集合S
    
            for(int v=1;v<=n;v++)
            {
                if(!vis[v]) dist[v]=min(dist[v],dist[u]+edge[u][v]); //对集合Q中,所有与点u相连接的点进行松弛
            }
        }
    }

    AC代码:

     1 #include<cstdio>
     2 #include<cmath>
     3 #define N 205
     4 #define INF 1e60
     5 double max(double a,double b){return a>b?a:b;}
     6 double edge[N][N],d[N];
     7 struct Point{
     8     int x,y;
     9 }point[N];
    10 int n;
    11 bool vis[N];
    12 double dist(Point a,Point b){
    13     return sqrt( (b.y-a.y)*(b.y-a.y) + (b.x-a.x)*(b.x-a.x) );
    14 } 
    15 void init()
    16 {
    17     for(int i=1;i<=n;i++)
    18     {
    19         if(i==1) d[1]=0.0 , vis[1]=1;
    20         else d[i]=dist(point[1],point[i]) , vis[i]=0;
    21         
    22         for(int j=i;j<=n;j++)
    23         {
    24             edge[i][j]=edge[j][i]=dist(point[i],point[j]);
    25         }
    26     }
    27 }
    28 double dijkstra()
    29 {
    30     for(int i=2;i<=n;i++)
    31     {
    32         double min=INF;int x;
    33         for(int j=1;j<=n;j++) if(!vis[j] && d[j] <= min) min=d[(x=j)];//找集合Q( Q=G.V - S )里d[x]最小的那个点x 
    34         vis[x]=1;//把点x放进集合S里 
    35         for(int y=1;y<=n;y++)//把在集合Q里所有与点x相邻的点都找出来松弛,因为这里青蛙可以在任意来两石头间跳,所以直接遍历 G.V - S 即可 
    36         {
    37             if(!vis[y]){
    38                 double tmp = max(d[x], edge[x][y]);
    39                 if(d[y] > tmp) d[y] = tmp;
    40             }
    41         }
    42     }
    43     return d[2];
    44 }
    45 int main()
    46 {
    47     int kase=0;
    48     while(scanf("%d",&n) && n!=0)
    49     {
    50         for(int i=1;i<=n;i++) scanf("%d%d",&point[i].x,&point[i].y);
    51         init();
    52         printf("Scenario #%d
    ",++kase);
    53         printf("Frog Distance = %.3f
    
    ",dijkstra());
    54     }
    55 }

    堆优化的Dijkstra算法:

    const int maxn=110;
    const int INF=0x3f3f3f3f;
    
    int n,m; //n个节点,m条边
    
    struct Edge
    {
        int u,v,w;
        Edge(int u,int v,int w){this->u=u,this->v=v,this->w=w;}
    };
    vector<Edge> E;
    vector<int> G[maxn];
    void init(int l,int r)
    {
        E.clear();
        for(int i=l;i<=r;i++) G[i].clear();
    }
    void addedge(int u,int v,int w)
    {
        E.push_back(Edge(u,v,w));
        G[u].push_back(E.size()-1);
    }
    
    bool vis[maxn];
    int d[maxn]; //标记是否在集合S中
    void dijkstra(int st)
    {
        for(int i=1;i<=n;i++) d[i]=(i==st)?0:INF;
        memset(vis,0,sizeof(vis));
    
        priority_queue< pair<int,int> > Q; //此处的Q即集合Q,只不过由于那些d[i]=INF根本不可能被选到,所以就不放到优先队列中
        Q.push(make_pair(0,st));
        while(!Q.empty())
        {
            int now=Q.top().second; Q.pop(); //选取集合Q中d[x]最小的那个点x
            if(vis[now]) continue; //如果节点x已经在集合S中,就直接略过
            vis[now]=1; //将节点x放到集合S中,代表节点x的d[x]已经计算完毕
            for(int i=0;i<G[now].size();i++) //松弛从节点x出发的边
            {
                Edge &e=E[G[now][i]]; int nxt=e.v;
                if(vis[nxt]) continue;
                if(d[nxt]>d[now]+e.w)
                {
                    d[nxt]=d[now]+e.w;
                    Q.push(make_pair(-d[nxt],nxt));
                }
            }
        }
    }
  • 相关阅读:
    突破极验验证的验证码
    c# 自定义多选下拉列表2
    c#多选下拉框(ComboBox)
    图片缩放+拖动(html)
    使用天天模拟器开发android应用
    FineUI开源版之TreeGrid(修改)
    FineUI开源版之TreeGrid实现
    c# 窗体最小化后截图实现
    c#一个简单的实例告诉你,多继承还可以这么来
    设置控件Enable=false,控件颜色不变
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804145.html
Copyright © 2020-2023  润新知