Ultraviolet A 1347 Tour DP, uva1347tourdp

Source: Internet
Author: User

Ultraviolet A 1347 Tour DP, uva1347tourdp


DP

Dp [I] [j] All the previous I points have passed. The backward person is on the j point.

Transfer dp [I + 1] [I] or dp [I + 1] [j] To point I + 1


Tour
Time Limit:3000 MS   Memory Limit:Unknown   64bit IO Format:% Lld & % llu

Submit Status

Description

John Doe, a skilled pilot, enjoys traveling. while on vacation, he rents a small plane and starts visiting beautiful places. to save money, John must determine the shortest closed tour that connects his destinations. each destination is represented by a point in the planePI = <XI,YI>. john uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. it is known that the points have distinctX-Coordinates.

Write a program that, given a setNPoints in the plane, computes the shortest closed tour that connects the points according to John's strategy.

Input

The program input is from a text file. each data set in the file stands for a participant set of points. for each set of points the data set contains the number of points, and the point coordinates in ascending order ofXCoordinate. White spaces can occur freely in input. The input data are correct.

Output

For each set of data, your program shocould print the result to the standard output from the beginning of a line. the tour length, a floating-point number with two fractional digits, represents the result.


Note:An input/output sample is in the table below. Here there are two data sets. The first one contains 3 points specified by theirXAndYCoordinates. The second point, for example, hasXCoordinate 2, andYCoordinate 3. The result for each data set is the tour length, (6.47 for the first data set in the given example ).

Sample Input

3 1 12 33 14 1 1 2 33 14 2

Sample Output

6.477.89

Source

Root: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim): Rare Topics: Rare Problems: Bitonic TSP
Root: aoapc ii: Beginning Algorithm Contests (Second Edition) (Rujia Liu): Chapter 9. Dynamic Programming: Examples
Root: aoapc I: Beginning Algorithm Contests -- Training Guide (Rujia Liu): Chapter 1. Algorithm Design: Dynamic Programming: Exercises: Intermediate

Submit Status


/*************************************** * ******** Author: CKbossCreated Time:, Saturday, February 07, 2015 File Name: UVA1347.cpp *************************************** * ********/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <cmath> # include <cstdlib> # include <vector> # include <queue> # include <set> # include <map> using namespace std; const int maxn = 1111; Int n; struct point {double x, y;} pt [maxn]; double dist (int a, int B) {double dx = fabs (pt [a]. x-pt [B]. x); double dy = fabs (pt [a]. y-pt [B]. y); return sqrt (dx * dx + dy * dy);} double dp [maxn] [maxn]; int main () {// freopen ("in.txt ", "r", stdin); // freopen ("out.txt", "w", stdout); while (scanf ("% d", & n )! = EOF) {for (int I = 0; I <n; I ++) scanf ("% lf", & pt [I]. x, & pt [I]. y); for (int I = 0; I <maxn; I ++) for (int j = 0; j <maxn; j ++) dp [I] [j] = (1LL <60); dp [0] [0] = 0; dp [1] [0] = dist (1, 0 ); for (int I = 1; I <n; I ++) {for (int j = 0; j <I; j ++) {dp [I + 1] [I] = min (dp [I + 1] [I], dp [I] [j] + dist (j, I + 1); dp [I + 1] [j] = min (dp [I + 1] [j], dp [I] [j] + dist (I, I + 1) ;}} double ans = 1LL <60; for (int I = 0; I <n; I ++) ans = min (ans, dp [n-1] [I] + dist (I, n-1); printf ("%. 2lf \ n ", ans);} 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.