UVA 1347(POJ 2677) Tour(双色欧几里德旅行商问题)


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

3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2

Sample Output

6.47
7.89

算是经典问题了吧。题目大意:给定平面上n个点的坐标(按x的升序),

你的任务是设计一条路线从最左边出发,到最右边再返回最左边,每点恰好经过一次。问最小代价。能够看成两个人从最左端同一时候出发。

每点恰有一人经过的最小代价。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<cmath>
typedef long long LL;
using namespace std;
const int maxn=110;
double x[maxn],y[maxn];
double dp[maxn][maxn];

double dis(int i,int j)
{
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}

int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&x[i],&y[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                dp[i][j]=INT_MAX;
        }
        dp[0][0]=0.0;//初始化
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<i;j++)
            {
               dp[i][i-1]=min(dp[i][i-1],dp[i-1][j]+dis(i,j));//当走到dp[i-1][j]要走到第i个点时。有两种途径
               dp[i][j]=min(dp[i][j],dp[i-1][j]+dis(i-1,i));//在i-1的人走到i或在j的人走到i;
            }
        }
//        for(int i=0;i<n;i++)
//            printf("fuck %d\n",dp[n-1][i]);
        double ans=dp[n-1][n-2]+dis(n-1,n-2);
        printf("%.2f\n",ans);
    }
    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-10-01 01:28:32

UVA 1347(POJ 2677) Tour(双色欧几里德旅行商问题)的相关文章

UVA 1347(POJ 2677)Tour(双调欧几里得旅行商问题)

Tour                 Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu 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 mus

uva 1347 poj 2267 Tour 最短双调回路

// uva1347 Tour 最短双调路线 // 这道题是看着紫书上面写着的 // dp[i][j]表示1至max(i,j)都已经走过时并且第一个人在i // 第二个人在j点时所要走的最短的距离,则dp[i][j] = dp[j][i] // 状态转移方程为 // dp[i+1][j] = max(dp[i][j]+dist[i][i+1],dp[i+1][i]+dist[j][i+1]) // 其实就是考虑第i+1号点是与i相连还是与j相连,(本来dp[i+1][i]是dp[i][i+1]

POJ 2677 Tour 双调旅行商 dp, double+费用流

题目链接:点击打开链接 题意:给定二维平面上的n个点 从最左端点到最右端点(只能向右移动) 再返回到到最右端点(只能向左移动,且走过的点不能再走) 问最短路. 费用流: 为了达到遍历每个点的效果 把i点拆成 i && i+n 在i ->i+n 建一条费用为 -inf 的边,流量为1 这样跑最短路时必然会经过这条边,以此达到遍历的效果. dp :点击打开链接 对于i点 :只能跟一个点相连 -- 1.跟 i-1点相连 2.不跟i-1相连 用dp[i][j] 表示两个线头为 i 和 j 的

[ACM] POJ 2677 Tour (动态规划,双调欧几里得旅行商问题)

Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3585   Accepted: 1597 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

poj 2677 Tour

Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4556   Accepted: 1993 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

POJ 2677 Tour DP

双调欧几里得问题,复习一下.. 把所有的点按照x排序,设计状态f(i,j)表示走在前面的那个走到了i点,后面那个在j点,所需的最小,那么转移显而易见. f(i,j) = min(f(i - 1,j) + dist(i,i - 1), f(i,i - 1) + dist(i,j)) 直接顺着推过去可能好理解一些.. 当前状态f(i,j) 如果由i走到i + 1,那么就是f(i + 1,j),否则就是f(i + 1,i).. #include <cstdio> #include <cstri

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

POJ 3177 边双连通求连通量度的问题

这道题的总体思路就是找到连通量让它能够看作一个集合,然后找这个集合的度,度数为1的连通量为k,那么需要添加(k+1)/2条边才可以保证边双连通 这里因为一个连通量中low[]大小是相同的,所以我们用ans[low[i]]++来计度数 这道题我最开始按学长的模板来写....MLE到哭了,也不知道这道题为什么这么逗,把5000的数组改成1000也能过,当然后来换了别的思路 为了防止重边的进入,开始设置了一个hash[][]二维数组来判断边是否已经存在,不额外添入 之后,我不采用二维数组,而是在get

《Node.js实战(双色)》作者之一——吴中骅访谈录

1 请和大家介绍下您及所从事的工作. 我目前在苏州唐人数码工作,是一家本地的网络游戏公司,主要经营棋牌游戏,最近自主研发了一款3D的网路游戏--争渡三国,我在公司负责游戏平台工作,对公司自营的游戏以及联运游戏提供Web支持,比如用户登录,游戏充值,平台接口服务等. 2 为什么会想到写这样一本书? 自从Node.js问世以来,它的发展速度相当迅猛,最早只有英文资料可以学习,现在中文的书籍也慢慢多了起来,但是这些学习教材绝大部分都是从基础的安装,hello world开始,然后进行对Node.js各