Title Link: https://cn.vjudge.net/problem/POJ-2253
Test instructions
A forg needs to go from node 1 to node n
Now we're going to find a path with the smallest intervals.
What is the minimum interval of the question?
Ideas
Just use Dijsktra.
Find the path with the smallest interval
- Notice the comparison of floating-point numbers
Code
#include <cstdio>#include <vector>#include <queue>#include <cmath>using namespaceStdConst intmaxn= $, inf=0x3f3f3f3f;Const Doubleeps=1e-6;typedefpair<Double,int> Pair;structnode{intx, y; Node (intx=0,inty=0): x (x), Y (y) {}}NODE[MAXN+5];structedge{intFrom, to; Edge (intFrom=0,intto=0): From, to {}};vector<edge> edges;vector<int> G[MAXN+5];DoubleDist[maxn+5];voidAddedge (intFromintTo) {Edges.push_back-Edge (from, to); G[from].push_back (Edges.size ()-1); G[to].push_back (Edges.size ()-1);}inline BOOLEqualConst Double&a,Const Double&B) {return(A-b<=eps && b-a<=eps);}inline DoubleDis (Const int&a,Const int&B) {returnsqrt ((node[a].x-node[b].x) * (node[a].x-node[b].x) + (NODE[A].Y-NODE[B].Y) * (NODE[A].Y-NODE[B].Y));DoubleDij (void){ for(intI=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;intFrom=x.second; for(intI=0; I<g[from].size (); i++) {Edge &e=edges[G[from][i]];intTo= (E.to==from) e.from:e.to;DoubleDis=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)); } }returndist[2];}intMainvoid){intN, cnt=1, X, y; while(SCANF ("%d", &n) = =1&& N) { for(intI=1; i<=n; i++) {scanf ("%d%d", &x, &y); Node[i]=node (x, y); for(intj=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 Find the path with the smallest interval