zoukankan      html  css  js  c++  java
  • POJ-2253-Frogger

    链接:https://vjudge.net/problem/POJ-2253

    题意:

    给n个点,求1->2的路径的每段路中的最大值的最短路。

    思路:

    刚开始看不懂题。看了题解后看明白的。

    直接Dijkstra算法,Dis数组保存到每点路径中的路段的最大值。

    代码:

    #include <iostream>
    #include <stack>
    #include <memory.h>
    #include <string>
    #include <algorithm>
    #include <math.h>
    #include <iomanip>
    using namespace std;
    const int MAXN = 200+10;
    double Map[MAXN][MAXN];
    double Dis[MAXN];
    int Vis[MAXN];
    int n;
    
    struct Node
    {
        double _x;
        double _y;
    }node[MAXN];
    
    double get_Len(Node a,Node b)
    {
        return sqrt((a._x-b._x)*(a._x-b._x) + (a._y-b._y)*(a._y-b._y));
    }
    
    double Dijkstra()
    {
        memset(Dis,0, sizeof(Dis));
        memset(Vis,0, sizeof(Vis));
        for (int i = 2;i<=n;i++)
        {
            Dis[i] = Map[1][i];
        }
        Vis[1] = 1;
        for (int i = 1;i<=n;i++)
        {
            int w;
            double small = 9999999.0;
            for (int j = 1; j <= n; j++)
            {
                if (Vis[j] == 0 && small > Dis[j])
                {
                    w = j;
                    small = Dis[j];
                }
            }
    
            Vis[w] = 1;
    
            if (w == 2)
                return small;
    
            for (int j = 1; j <= n; j++)
            {
                if (Vis[j] == 0)
                {
                    //double now = max(small,Map[w][j]);
                    //if (now < Dis[j])
                        //Dis[j] = now;
                    Dis[j] = min(Dis[j], max(small, Map[w][j]));
                }
            }
        }
    }
    
    int main()
    {
        int cnt = 0;
        while (cin >> n && n)
        {
            for (int i = 1;i<=n;i++)
            {
                cin >> node[i]._x;
                cin >> node[i]._y;
            }
            for (int i = 1;i<=n;i++)
                for (int j = 1;j<=n;j++)
                    Map[i][j] = Map[j][i] = get_Len(node[i],node[j]);
    
            double v = Dijkstra();
            if (cnt != 0)
                printf("
    ");
            printf("Scenario #%d
    Frog Distance = %.3f
    ",++cnt,v);
        }
    }
    

      

  • 相关阅读:
    【算法笔记】B1015 德才论
    【算法笔记】B1014 福尔摩斯的约会
    【算法笔记】B1013 数素数
    【算法笔记】B1012 数字分类
    【算法笔记】B1011 A+B 和 C
    【算法笔记】B1010 一元多项式求导
    【算法笔记】B1009 说反话
    【算法笔记】B1008 数组元素循环右移问题
    SSLOJ 1336.膜拜神牛
    SSLOJ 1335.蛋糕切割
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10272219.html
Copyright © 2011-2022 走看看