Test instructions: Give you a set of points on a plane (x values are unequal), ask you to go from the leftmost to the far right (only in the order of x increments), and then from the far right to the leftmost (in descending order of x) ask you what the shortest distance is.
Problem-solving ideas: dp[i][j] means that one of the roads has reached the shortest distance I have another road at J.
Problem Solving Code:
1 //File name:c.1.cpp2 //Author:darkdream3 //Created time:2015 March 17 Tuesday 09:15 20 seconds4 5#include <vector>6#include <list>7#include <map>8#include <Set>9#include <deque>Ten#include <stack> One#include <bitset> A#include <algorithm> -#include <functional> -#include <numeric> the#include <utility> -#include <sstream> -#include <iostream> -#include <iomanip> +#include <cstdio> -#include <cmath> +#include <cstdlib> A#include <cstring> at#include <ctime> - #defineLL Long Long - - using namespacestd; - intT,n; - structpoint{ in intx, y; -}p[555]; to Doubledp[555][555]; + DoubleDisintAintb) - { the returnsqrt1.0* (p[a].x-p[b].x) * (p[a].x-p[b].x) +1.0* (P[A].Y-P[B].Y) * (P[A].Y-p[b].y)); * } $ intMain () {Panax Notoginsengscanf"%d",&t); - while(t--) the { +scanf"%d",&n); A for(inti =1; I <= N;i + +) the { +scanf"%d%d",&p[i].x,&p[i].y); - } $ if(n = =1) $ { -printf"%f",0.0); - Continue; the } -dp[2][1] = DIS (1,2);Wuyi for(inti =2; i < N;i + +) the { -dp[i+1][i] =100000000; Wu for(intj =1; J < I; J + +) - { Aboutdp[i+1][J] = dp[i][j] + dis (i,i+1); $dp[i+1][i] = min (dp[i+1][I],DP[I][J] + dis (j,i+1)); - } - } -printf"%f\n", dp[n][n-1] + DIS (n/A1)); A } + return 0; the}View Code
GCPC2014 C Bounty Hunter