题意:双调欧几里得旅行商问题。算法导论15-1题,从最左边的点严格从左走到右再从右走到左回到起点,所有点都要走且只走一次,求最短路径。

解法:定义dp[i][j]表示从i走到j的双调路径,分为两种情况:

  1. 当j < i - 1时,dp[i][j] = dp[i - 1][j] + dis[i - 1][i]
  2. 当j = i - 1时,dp[i][j] = min{dp[j][k] + dis[j][k]}

最初的情况是dp[2][1] = dis[2][1]。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
struct node
{
int x, y;
bool operator < (const node& tmp) const
{
if(x == tmp.x)
return y < tmp.y;
else
return x < tmp.x;
}
} point[2005];
double dp[2005][2005];
double dis[2005][2005];
int main()
{
//freopen("in.txt", "r", stdin);
int n;
while(~scanf("%d", &n))
{
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++)
scanf("%d%d", &point[i].x, &point[i].y);
sort(point, point + n);
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++)
{
double d = sqrt((point[i].x - point[j].x) * (point[i].x - point[j].x) + (point[i].y - point[j].y) * (point[i].y - point[j].y));
dis[i][j] = dis[j][i] = d;
}
dp[1][0] = dis[1][0];
for(int i = 2; i < n; i++)
{
for(int j = 0; j < i - 1; j++)
dp[i][j] = dp[i - 1][j] + dis[i][i - 1];
dp[i][i - 1] = 1e10;
for(int j = 0; j < i - 1; j++)
dp[i][i - 1] = min(dp[i][i - 1], dp[i - 1][j] + dis[j][i]);
}
if(n == 2)
dp[1][1] = dis[1][0];
else
dp[n - 1][n - 1] = dp[n - 1][n - 2] + dis[n - 2][n - 1];
printf("%.2lf\n", dp[n - 1][n - 1]);
}
return 0;
}

  

POJ 2677 Tour的更多相关文章

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

    Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane a ...

  2. POJ Farm Tour

    Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...

  3. 最小费用最大流模板(POJ 2135-Farm Tour)

    最近正好需要用到最小费用最大流,所以网上就找了这方面的代码,动手写了写,先在博客里存一下~ 代码的题目是POJ2135-Farm Tour 需要了解算法思想的,可以参考下面一篇文章,个人觉得有最大流基 ...

  4. POJ 2677 旅行商问题 双调dp或者费用流

    Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3408   Accepted: 1513 Description ...

  5. POJ 2135_Farm Tour

    题意: 从出发点到目的地往返一次,道路i连接着ai号和bi号,长度为ci.同一条路只能走一次,求最小路径长度. 分析: 如果没有往返,那么就是简单的最短路问题.如果规定严格从左到右,那么就是简单的双调 ...

  6. ACM - 动态规划专题 题目整理

    CodeForces 429B  Working out 预处理出从四个顶点到某个位置的最大权值,再枚举相遇点,相遇的时候只有两种情况,取最优解即可. #include<iostream> ...

  7. [SinGuLaRiTy] 动态规划题目复习

    [SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metr ...

  8. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  9. POJ 1637 Sightseeing tour(最大流)

    POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...

随机推荐

  1. Head First设计模式悟道

    暂时包括 策略模式,观察者,装饰模式,工厂模式,抽象工厂模式,后续会继续补充中,纯属个人总结用,不喜勿喷, 源代码见: 传送门 public class NYPizzaIngredientFactor ...

  2. java8 新特性

    [转载]:http://www.importnew.com/11908.html 本文由 ImportNew - 刘 家财 翻译自 javacodegeeks.欢迎加入翻译小组.转载请见文末要求. 编 ...

  3. 1053: [HAOI2007]反素数ant - BZOJ

    Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数 ...

  4. [转载]C# 中Web.config文件的读取与写入

    asp.net2.0新添加了对web.config直接操作的功能.开发的时候有可能用到在web.config里设置配置文件,其实是可以通过程序来设置这些配置节的. asp.net2.0需要添加引用: ...

  5. [转载]JS对URL的编码

    虽然escape().encodeURI().encodeURIComponent()三种方法都能对一些影响URL完整性的特殊字符进行过滤.但后两者是将字符串转换为UTF-8的方式来传输,解决了页面编 ...

  6. Machine Learning for Developers

    Machine Learning for Developers Most developers these days have heard of machine learning, but when ...

  7. PAT-乙级-1032. 挖掘机技术哪家强(20)

    1032. 挖掘机技术哪家强(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 为了用事实说明挖掘机技术到底 ...

  8. js的原型链

    js中的原型链是一个很重要的概念,理解了原型链,对js程序的开发有很大的好处,废话不说,先上图: javascript是基于原型的语言,所以一个对象可以另一个对象继承.不过javascript实现的时 ...

  9. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  10. php的redis 操作类,适用于单台或多台、多组redis服务器操作

    redis 操作类,包括单台或多台.多组redis服务器操作,适用于业务复杂.高性能要求的 php web 应用. redis.php: <?php /* redis 操作类,适用于单台或多台. ...