zoukankan      html  css  js  c++  java
  • POJ-2253-Frogger +最短路小变形

    传送门:http://poj.org/problem?id=2253

    参考:https://www.cnblogs.com/lienus/p/4273159.html

    题意:给出一个无向图,求一条从 1 到 2 的路径,使得路径上的最大边权最小;

    思路:

    dij将距离更新改成取最大值即可,即dis[i]表示到达i点过程中的最大边权,更新后可能多个,再靠优先队列取出最小的最大边权。

    #include <iostream>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define pb push_back
    #define fi first
    #define se second
    typedef long long ll;
    
    const ll INF = 1e9+9 ;
    const int maxn  = 1006 ;
    
    ll n, m, k, s;
    double dis[maxn];
    bool vis[maxn];
    vector < pair<ll, double > > mp[maxn];
    priority_queue< pair<double ,ll > > q;
    
    struct node {
        int x,y;
    }ac[300];
    double omyway(int i,int j)
    {
        node a = ac[i],b = ac[j];
        return sqrt( 1.0*(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
    }
    void dij(){
        memset(vis,0,sizeof(vis));
        while(!q.empty())
        {
                int v = q.top().se;
                q.pop();
                if(vis[v])continue;
                vis[v] = 1;
                for(int i=0;i<mp[v].size();i++)
                {
                    int tmp = mp[v][i].fi;
                    double tmpc = mp[v][i].se;
                    if(dis[tmp] > max(dis[v],tmpc))
                    {
                        dis[tmp] = max(dis[v] , tmpc);
                        q.push(make_pair(-1.0*dis[tmp],tmp));
                    }
                }
        }
    }
    int main(){
        int cnt = 1;
        while(~scanf("%lld", &n) && n)
        {
    
            for(int i = 0; i < maxn; i ++)
                mp[i].clear(), dis[i]=INF;
    
            for(int i=1; i<=n; i++)
            {
               int x,y;
               scanf("%d%d",&x,&y);
               ac[i].x=x;
               ac[i].y=y;
            }
    
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    double tmp = omyway(i,j);
                    mp[i].push_back(make_pair(j,tmp));
                    mp[j].push_back(make_pair(i,tmp));
                }
            }
    
            dis[1] = 0.0;
            q.push(make_pair(0.0,1));
            dij();
            printf("Scenario #%d
    ",cnt++);
            printf("Frog Distance = %.3f
    
    ",dis[2]);
        }
        return 0;
    }
  • 相关阅读:
    ajax01
    django04
    数据库
    WeakHashMap类
    IdentityHashMap
    Hashtable类
    LinkedHashMap类
    HashMap和TreeMap类
    PriorityQueue
    Synchronized
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/8747108.html
Copyright © 2011-2022 走看看