码迷,mamicode.com
首页 > Web开发 > 详细

POJ-2253 Frogger dijsktra查找间隔最小的路径

时间:2018-05-26 21:20:56      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:namespace   priority   代码   vector   org   submit   注意   lan   node   

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

题意

一只Forg需要从节点1走到节点n
现要找一条各个间隔最小的路径
问间隔最小是多少

思路

用dijsktra就好
查找间隔最小的路径

  1. 注意浮点数的比较

代码

#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
using namespace std;
const int maxn=200, INF=0x3f3f3f3f;
const double eps=1e-6;
typedef pair<double, int> Pair;
struct Node{
    int x, y;
    Node(int x=0, int y=0):x(x), y(y) {}
}node[maxn+5];
struct Edge{
    int from, to;
    Edge(int from=0, int to=0):from(from), to(to) {}
};
vector<Edge> edges;
vector<int> G[maxn+5];
double dist[maxn+5];

void addEdge(int from, int to){
    edges.push_back(Edge(from, to));
    G[from].push_back(edges.size()-1);
    G[to].push_back(edges.size()-1);
}

inline bool equal(const double &a, const double &b){
    return (a-b<=eps && b-a<=eps);
}

inline double Dis(const int &a, const int &b){
    return sqrt((node[a].x-node[b].x)*(node[a].x-node[b].x)+
        (node[a].y-node[b].y)*(node[a].y-node[b].y));
}

double dij(void){
    for (int i=0; i<=maxn; i++) dist[i]=INF;
    priority_queue<Pair, vector<Pair>, greater<Pair> > que;
    que.push(Pair(0, 1)); dist[1]=0;


    while (que.size()){
        Pair x=que.top(); que.pop();
        if (!equal(x.first, dist[x.second])) continue;

        int from=x.second;
        for (int i=0; i<G[from].size(); i++){
            Edge &e=edges[G[from][i]];
            int to=(e.to==from)?e.from:e.to;
            double dis=Dis(to, from), mdis=max(dist[from], dis);

            if (dist[to]<mdis || equal(dist[to], mdis)) continue;
            dist[to]=mdis;
            que.push(Pair(dist[to], to));
        }
    }return dist[2];
}

int main(void){
    int n, cnt=1, x, y;

    while (scanf("%d", &n)==1 && n){
        for (int i=1; i<=n; i++){
            scanf("%d%d", &x, &y);
            node[i]=Node(x, y);
            for (int j=1; j<i; j++) addEdge(i, j);
        }
        printf("Scenario #%d\nFrog Distance = %.3f\n\n", cnt++, dij());
    }

    return 0;
}
Time Memory Length Lang Submitted
16ms 1476kB 1878 G++ 2018-05-23 15:31:25

POJ-2253 Frogger dijsktra查找间隔最小的路径

标签:namespace   priority   代码   vector   org   submit   注意   lan   node   

原文地址:https://www.cnblogs.com/tanglizi/p/9094184.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!