TSP是NP难,但是把问题简化,到最右点之前的巡游路线只能严格向右,到最右边的点以后,返回的时候严格向左,这个问题就可以在多项式时间内求出来了。

定义状态d[i][j]表示一个人在i号点,令一个人在j号点,之前的点全走过到终点还要走的最小长度,为了区别d[i][j]和d[j][i]规定i>j,然后考虑下一个点一定是要走的

只要考虑是和i相连还是和j相连

#include<bits/stdc++.h>
using namespace std; const int maxn = ;
int x[maxn],y[maxn]; double dp[maxn][maxn];
bool vis[maxn][maxn];
#define Squ(x) ((x)*(x)) inline double dist(int a,int b)
{
return sqrt(Squ(x[a]-x[b])+Squ(y[a]-y[b]));
} int n;
double dfs(int i,int j)
{
if(vis[i][j]) return dp[i][j];
vis[i][j] = true;
if(i == n-){ return dp[i][j] = dist(i,n)+dist(j,n);
}
return dp[i][j] = min(dfs(i+,i)+dist(j,i+),dfs(i+,j)+dist(i,i+));
} int main()
{
// freopen("in.txt","r",stdin);
while(~scanf("%d",&n)){
for(int i = ; i <= n; i++) scanf("%d%d",x+i,y+i);
memset(vis,,sizeof(vis));
double ans = dfs(,);
printf("%.2lf\n",ans);
}
return ;
}

UVA 1347 Tour 双调TSP的更多相关文章

  1. ACM - 动态规划 - UVA 1347 Tour

    UVA 1347 Tour 题解 题目大意:有 \(n\) 个点,给出点的 \(x\).\(y\) 坐标.找出一条经过所有点一次的回路,从最左边的点出发,严格向右走,到达最右点再严格向左,回到最左点. ...

  2. UVA 1347 Tour 【双调旅行商/DP】

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

  3. UVA - 1347 Tour(DP + 双调旅行商问题)

    题意:给出按照x坐标排序的n个点,让我们求出从最左端点到最右短点然后再回来,并且经过所有点且只经过一次的最短路径. 分析:这个题目刘汝佳的算法书上也有详解(就在基础dp那一段),具体思路如下:按照题目 ...

  4. UVa 1347 Tour

    Tour Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description   Joh ...

  5. UVA 1347"Tour"(经典DP)

    传送门 参考资料: [1]:紫书 题意: 欧几里得距离???? 题解: AC代码: #include<bits/stdc++.h> using namespace std; ; int n ...

  6. Tour UVA - 1347

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

  7. 【UVa 1347】Tour

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

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

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

  9. UVa 1347 (双线程DP) Tour

    题意: 平面上有n个坐标均为正数的点,按照x坐标从小到大一次给出.求一条最短路线,从最左边的点出发到最右边的点,再回到最左边的点.除了第一个和最右一个点其他点恰好只经过一次. 分析: 可以等效为两个人 ...

随机推荐

  1. 2.1-2.2 HBase数据存储

    一.HBase数据检索流程 一篇介绍HBase数据读写流程的解析的博文:http://hbasefly.com/2016/12/21/hbase-getorscan/?wsfatm=uqvhl3 1. ...

  2. PHP实用小程序(七)

    <? //用COOKIE保存投票人的投票记录 if($vote && !$already_voted) SetCookie("already_voted",& ...

  3. UVa 1641 ASCII Area (计算几何,水题)

    题意:给定一个矩阵,里面有一个多边形,求多边形的面积. 析:因为是在格子里,并且这个多边形是很规则的,所以所有格子不是全属于多边形就是全不属于,或者一半,并且我们可以根据"/"和“ ...

  4. POJ - 3494 Largest Submatrix of All 1’s 单调栈求最大子矩阵

    Largest Submatrix of All 1’s Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is ...

  5. Spring Boot 学习系列(09)—自定义Bean的顺序加载

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. Bean 的顺序加载 有些场景中,我们希望编写的Bean能够按照指定的顺序进行加载.比如,有UserServ ...

  6. C# 面向对象之继承

    Object是所有类的基类 1.在C#中一个类可以继承另一个类(密封类除外,静态类是密封的不能被继承); 2.被继承的类被成为基类(父类);继承的类被成为派生类(子类); 3.子类将获得父类除构造函数 ...

  7. 洛谷 P1053 篝火晚会

    https://www.luogu.org/problemnew/show/P1053 错误记录:判-1的时候出了些问题(比如只判了图是否连通):数组没清空 #include<cstdio> ...

  8. oracle tps

    http://blog.csdn.net/nilxin/article/details/5812480 sample 1: 定义 TPS:Transactions Per Second(每秒传输的事物 ...

  9. 535 Encode and Decode TinyURL 编码和解码精简URL地址

    详见:https://leetcode.com/problems/encode-and-decode-tinyurl/description/ C++: class Solution { public ...

  10. SSH的端口转发

    这里是一篇很好的介绍SSH PortForwarding的文章http://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/ 可以将远端服务器一 ...