Floyd-warshall algorithm (solves the shortest path between any two points) detailed + deformation of the POJ 2253 Frogger

Source: Internet
Author: User

/* For a long time did not do the problem of graph theory, review. ---------------------------------------------------------Shortest-circuiting (Floyd-warshall algorithm) dynamic programming between any two points: dp[k][i][j] :=   node I can reach the shortest path of the J-node through a node numbered 1,2...K. The use of 1,2...K nodes, can be divided into the following two scenarios to discuss: (1) I to j the shortest path through the node K once Dp[k-1][i][k] + dp[k-1][k][j] (2) I to j the shortest path completely without the node Kdp[k-1] I [j] therefore: Dp[k][i][j] = min (Dp[k-1][i][k] + dp[k-1][k][j], dp[k-1][i][j]) space is optimized: Dp[i][j]  = min (Dp[i][k] + dp[k][j], dp[i][j]) initial value:dp[0][i][j] = 0  (i ==  J) The weight of the dp[0][i][j] :=  edge i->j if there is no edge,dp[i][j] = inf  (0x3f3f3f3f) time complexity between I and J: O (| V|^3) can handle the case that the edge is negative, judge whether there is a negative circle in the graph, just check if there is a dp[i][i] is a negative vertex I can be. ---------------------------------------------------------------------------------poj 2253  Frogger for this problem, ask: 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 asthe minimum necessary jump  range over all possible paths between the two stones.  namely:a path needs to jump many times, but there is a jump in all jumps need to jump the longest distance, this distance can be called the longest distance to jump in this path, find a stone to reach the B stone all the path of the need to jump the longest distance, where the shortest distance is the answer, namely: the Frog Distance The Floyd-warshall algorithm traverses all paths to the i->j, so we can change the DP state to solve this problem: dp[k][i][j]: = node I can reach the J-node path through the numbered 1,2...K node, the shortest maximum distance required to jump. So: dp[k][i][j] = min (Dp[k-1][i][j], Max (Dp[k-1][i][k], dp[k-1][k][j])) Optimize: dp[i][j]: = Min (Dp[i][j], Max (dp[i][k), dp[k ][J])) Initial value: Dp[0][i][j]: = Distance from node i to node J. */
1#include <iostream>2#include <cstdlib>3#include <cstdio>4#include <cstddef>5#include <iterator>6#include <algorithm>7#include <string>8#include <locale>9#include <cmath>Ten#include <vector> One#include <cstring> A#include <map> -#include <utility> -#include <queue> the#include <stack> -#include <Set> - using namespacestd; - Const intINF =0x3f3f3f3f; + Const intMAXN =205; - Const intModprime =3046721; +  A structNode at { -     Doublex, y; - }; -  - intN; - Node NODESET[MAXN]; in DoubleDP[MAXN][MAXN]; - DoubleDIS[MAXN][MAXN]; to  + voidgetdistance () - { the      for(inti =1; I <= N; ++i) *     { $          for(intj =1; J <= N; ++j)Panax Notoginseng         { -             if(I! =j) the             {     +DIS[I][J] = sqrt ((nodeset[i].x-nodeset[j].x) * (nodeset[i].x-nodeset[j].x) + A(NODESET[I].Y-NODESET[J].Y) * (NODESET[I].Y-nodeset[j].y)); theDis[j][i] =Dis[i][j]; +             } -             Else $             { $DIS[I][J] =0.0; -             } -         } the     } - }Wuyi  the voidSolve () - { Wu      for(inti =1; I <= N; ++i) -     { About          for(intj =1; J <= N; ++j) $         { -DP[I][J] =Dis[i][j]; -         } -     } A      for(intK =1; K <= N; ++k) +     { the          for(inti =1; I <= N; ++i) -         { $              for(intj =1; J <= N; ++j) the             { theDP[I][J] =min (dp[i][j], Max (Dp[i][k], dp[k][j])); the             } the         } -     } inprintf"Frog Distance =%.3lf\n\n", dp[1][2]); the  the } About  the intMain () the { the #ifdef HOME +Freopen ("inch","R", stdin); -     //freopen ("Out", "w", stdout); the #endifBayi  the     intnum =1; the      while(~SCANF ("%d", &n) &&N) -     { -          for(inti =1; I <= N; ++i) the         { thescanf"%LF%LF", &nodeset[i].x, &nodeset[i].y); the         } the getdistance (); -printf"Scenario #%d\n", num); the Solve (); the++num; the     }94  the  the #ifdef HOME theCerr <<"Time Elapsed:"<< clock ()/Clocks_per_sec <<"Ms"<<Endl;98 _CrtDumpMemoryLeaks (); About #endif -     return 0;101}


Floyd-warshall algorithm (solves the shortest path between any two points) detailed + deformation of the POJ 2253 Frogger

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.