Dynamic Planning of HDU-2059 Tortoise and rabbit race

Source: Internet
Author: User

Given a rule to ask the result of a race, the question is when the tortoise chooses to charge.

Solution: because there are only 100 charging stations at most, the decision affecting the result takes place in each charging station. Therefore, you only need to consider the strategy to be adopted when arriving at the charging station. You only need to retain the optimal value for a certain station and charge it.

The Code is as follows:

# Include <iostream> # include <algorithm> using namespace STD; // if the two nodes are connected to the edge, class rule {public: static int n must be added to the points on the arc head; // Number of charging stations static int VR, vt1, VT2; // three speed values static int efar; // The distance between the charging station and the static int ETI; // The charging time is static int length; // The length of the track}; int rule: vr, rule: vt1, rule: VT2, rule: N; int rule:: efar, rule: ETI, rule: length; Class node {PRIVATE: int X; // double Ti at the coordinate position; // bool vis; // Whether the point has reached public: node () {vis = false;} int getx () const {return X;} double getti () const {return Ti;} bool getvis () const {return vis;} void setx (INT v) {x = V;} void setti (Double V) {Ti = V;} void setvis (bool V) {vis = V;} void read () {CIN> X;} // read the position of the charging station: Friend double cost (const node &, const node &) ;}; double cost (const node & A, const node & B) {int Dist = B. getx ()-. getx (); If (Dist <= Rule: efar) return rule: ETI + (1.0 * Dist/rule: vt1); else return rule: ETI + (1.0 * Rule: efar/rule:: vt1 + 1.0 * (Dist-rule: efar)/rule: VT2);} void solve (node Nd [], int maxn) {for (INT I = 0; I <= rule: N; ++ I) {// enumeration starts if (! Nd [I]. getvis () continue; // If the vertex has not been updated, it will not update other vertices for (Int J = I + 1; j <maxn; ++ J) {// The Point J that can be reached from point I is the point if (Nd [J]. getvis () {// if this point has been accessed by Nd [J]. setti (min (Nd [J]. getti (), Nd [I]. getti () + cost (Nd [I], Nd [J]);} else {Nd [J]. setti (Nd [I]. getti () + cost (Nd [I], Nd [J]); Nd [J]. setvis (true) ;}}} bool win (node Nd []) {double A = 1.0 * Rule: length/rule: vr; Double B = Nd [Rule:: n + 1]. getti ()-R Ule: ETI; return B <A;} int main () {While (CIN> rule: length) {CIN> rule: n> rule :: efar> rule: ETI; CIN> rule: vr> rule: vt1> rule: VT2; const int maxn = rule: n + 2; node Nd [maxn]; // call the initialization function for each data record (INT I = 1; I <= rule: N; ++ I) {Nd [I]. read ();} Nd [0]. setx (0); // construct the start point and end point Nd [0]. setti (0.0); Nd [0]. setvis (true); Nd [Rule: n + 1]. setx (rule: length); solve (Nd, maxn); cout <(WIN (ND)? "What a pity rabbit! \ N ":" Good job, Rabbit! \ N ") ;}return 0 ;}

 

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.