Hdu 2224 The shortest path(双调TSP)

2024-05-10 16:32
文章标签 path hdu shortest tsp 双调 2224

本文主要是介绍Hdu 2224 The shortest path(双调TSP),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2224 

The shortest path

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 654    Accepted Submission(s): 335


Problem Description
There are n points on the plane, Pi(xi, yi)(1 <= i <= n), and xi < xj (i<j). You begin at P1 and visit all points then back to P1. But there is a constraint:
Before you reach the rightmost point Pn, you can only visit the points those have the bigger x-coordinate value. For example, you are at Pi now, then you can only visit Pj(j > i). When you reach Pn, the rule is changed, from now on you can only visit the points those have the smaller x-coordinate value than the point you are in now, for example, you are at Pi now, then you can only visit Pj(j < i). And in the end you back to P1 and the tour is over.
You should visit all points in this tour and you can visit every point only once.


Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n(2 <= n <= 200), means the number of points. Then following n lines each containing two positive integers Pi(xi, yi), indicating the coordinate of the i-th point in the plane.


Output
For each test case, output one line containing the shortest path to visit all the points with the rule mentioned above.The answer should accurate up to 2 decimal places.


Sample Input
  
3 1 1 2 3 3 1


Sample Output
  
6.47Hint: The way 1 - 3 - 2 - 1 makes the shortest path.



题意:给n个二维点,你要先沿x坐标严格递增的方向从1点走到n点,然后在沿x坐标严格递减的方向从n点走到1点,求走过的路程和的最小值。

分析:可以将上述的一条路看成从1点到n点的没有交叉点的两条路A和B。 两条路分别从 点1 向x递增的方向 A走到 i,B走到 j 。

就可以定义 dp[i][j] 为A走到 i ,B走到 j 的最短距离。题目就变成了 求dp[n][n]的值。

转移方程:

dp[1][2] = D[i][j]; //i ,j 两点的真实距离。

i < j-1 时; dp[ i ][ j ] = dp[ i ][ j-1] + D[ j-1 ][ j ];

i == j-1 时;dp[ i ][ j ] = min(dp[ k ][ j-1 ] + D[ k ][ j ]);

dp[ n ][ n ] = dp[ n-1 ][ n ]+ D[ n-1 ][ n ].

代码:

  

#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX 205
#define inf 9999999
double dp[MAX][MAX];
double D[MAX][MAX];
struct node
{double x,y;
}p[MAX];
int cmp(const void *a,const void *b)
{struct node *p = (struct node *)a;struct node *q = (struct node *)b;return (p->x - q->x >0)?1:-1;
}
double dis(node a,node b)
{return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
void init(int n)
{for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++){D[i][j] = inf;}
}
int main()
{int n;while(scanf("%d",&n)!=EOF){init(n);for (int i = 1;i<=n;i++){scanf("%lf %lf",&p[i].x,&p[i].y);for(int j=1;j<i;j++)D[i][j]=D[j][i] = dis(p[i],p[j]);//记录直接距离。}qsort(p+1,n,sizeof(p[1]),cmp);dp[1][2] = D[1][2];for (int i = 3; i <= n; i++){for (int j = 1; j <= i-2; j++)//when i-1 in the higher dir{dp[j][i] = dp[j][i-1] + D[i-1][i];}double Min = inf;for (int j = 1; j <= i-2; j++)//when i-1 in the lower dir{Min = min(dp[j][i-1]+D[j][i],Min);}dp[i-1][i] = Min;}dp[n][n] = dp[n-1][n]+D[n][n-1];printf("%.2lf\n",dp[n][n]);}return 0;
}


这篇关于Hdu 2224 The shortest path(双调TSP)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/976971

相关文章

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置

python中os.stat().st_size、os.path.getsize()获取文件大小

《python中os.stat().st_size、os.path.getsize()获取文件大小》本文介绍了使用os.stat()和os.path.getsize()函数获取文件大小,文中通过示例代... 目录一、os.stat().st_size二、os.path.getsize()三、函数封装一、os

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin