HDU - 2224 The shortest path (双调欧几里德旅行商问题)
来源:互联网 发布:编程资料 编辑:程序博客网 时间:2024/05/19 19:57
解题思路:可以去看一下有关于双调欧几里德旅行商问题的文章,我这里只是讲解一下为什么最后输出的是dp[n][n-1] + dis[n][n-1],解决上次没有解决的问题
本来到最后面,要求的dp[n][n] = min(dp[i][n] + dis[i][n])
因为 dp[i][n] = dp[i][n - 1] + dis[n][n-1]
所以 dp[n][n] = min(dp[i][n - 1] + dis[n][n-1] + dis[i][n])
又因为dp[n][n-1] = min(dp[i][n-1] + dis[i][n]),且dp[n][n-1]前面已经求到
所以dp[n][n] = dp[n][n - 1] + dis[n][n-1]
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 210const double INF = 0x3f3f3f3f3f3f3f3f;struct Point { int x, y;}P[N];double dp[N][N];double dis[N][N];int n;double len(Point a, Point b) { return sqrt((a.x - b.x) * (a.x - b.x) * 1.0 + (a.y - b.y) * (a.y - b.y) * 1.0);}void init() { for (int i = 1; i <= n; i++) scanf("%d%d", &P[i].x, &P[i].y); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { dis[i][j] = len(P[i], P[j]); }}void solve() { dp[2][1] = dp[1][2] = dis[1][2]; for (int i = 3; i <= n; i++) { dp[i][i - 1] = INF; for (int j = 1; j < i - 1; j++) { dp[i][j] = dp[i - 1][j] + dis[i - 1][i]; dp[i][i - 1] = min(dp[i][i-1], dp[i-1][j] + dis[j][i]); } } printf("%.2lf\n", dp[n][n-1] + dis[n][n - 1]);}int main() { while (scanf("%d", &n) != EOF) { init(); solve(); } return 0;}
0 0
- 双调欧几里德旅行商问题--hdu 2224 The shortest path --- POJ 2677Tour
- HDU - 2224 The shortest path (双调欧几里德旅行商问题)
- HDU 2224 The shortest path 双调旅行商问题
- HDU 2224 The shortest path(双调欧几里得旅行商问题)
- hdu 2224 The shortest path 双调欧几里得旅行商问题(动态规划)
- hdu 2224 The shortest path(双调欧几里得旅行商问题)
- hdu 2224 The shortest path 单向旅行商问题
- HDU 2224 The shortest path_TSP旅行商问题_DP
- hduThe shortest path(双调欧几里得旅行商问题)
- HDU2224The shortest path(双调旅行商问题)经典
- hdu2224The shortest path(双调旅行商问题)
- HDU 2224 The shortest path
- hdu 2224 The shortest path
- HDU 2224 The shortest path
- HDU 2224 The shortest path
- HDU-2224-The shortest path
- 双调欧几里德旅行商问题
- 双调欧几里德旅行商问题
- 哎!不愧是大神啊
- RHEL5.2内核升级过程
- Oracle session nls_date_format
- 盗梦空间
- ListActivity中android:id="@+id/android:list"
- HDU - 2224 The shortest path (双调欧几里德旅行商问题)
- 基于XMPP的即时通讯工具(一)
- Java应用打包后运行需要注意编码问题
- Angular的一些知识点
- MyEclipse2014快速配置Spring & Spring Testing, Spring AOP简单使用
- 天声人語 20150724
- JAVA流操作(2)文件流
- Lowest Common Ancestor of a Binary Search Tree
- oracle 数据库设计的一些原则