天天看點

POJ 2677 旅行商問題 雙調dp或者費用流

Tour

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 3408

Accepted: 1513

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 plane pi = < 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 distinct x-coordinates.

Write a program that, given a set of n points 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 particular set of points. For each set of points the data set contains the number of points, and the point coordinates in ascending order of the x coordinate.

White spaces can occur freely in input. The input data are correct.

Output

For each set of data, your program should 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. An input/output sample

is in the table below. Here there are two data sets. The first one contains 3 points specified by their x and y coordinates. The second point, for example, has the x coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47

for the first data set in the given example).

Sample Input

Sample Output

一旅行商從左向右走到最右邊,然後再傳回原來出發點的最短路徑。

兩種做法,第一種dp,dp[i][j]表示以i,j結尾的兩條不相交的路徑假設i一定大于j,i有兩種選擇,與i-1相連,不與i-1相連,然後dp

代碼:

費用流,把每一個點拆點,中間連流量為1,費用為負無窮的邊,代表該點必須選擇,兩兩之間連流量為1,費用為兩點距離的邊,起點,終點連邊,流量為1,費用為0.

代表可以增廣兩次。