Chapter_9 DP : uva1347 tour (bitonic tour)
https://cn.vjudge.net/problem/UVA-1347
这道题居然可以O(n^2)解决, 让我太吃惊了!!!
鄙人见识浅薄, 这其实是一个经典问题: bitonic tour.
它的定义是:
从最左点走到最右点在走回来, 不重复经过点, 最小需要多少路程.
在最左点走到最右点的过程中, 只走到比当前点x坐标大的点, 反之同理. (在该题中, 没有两个点x坐标重复)
要得出\(O(n^2)\)的DP算法, 需要几步转化:
首先, 计算从左到右再回来的路径长度很麻烦(因为这样回来时要标记所有走过的点, 状态\(2^n\)), 不可行.
可以看成有两个人从最左点出发, 经过不同的路径, 最后都走到了最右点.
然后, 为了防止集合的标记, 我们定义以下状态:
不妨设i>j.(请思考)
\]
这样我们就无需标记经过的点了.
因为每次每个人都在向右走, 所以只要讨论一下是那个人走到了\(i+1\)就可以了.
这就是状态转移方程:
f[i+1][i] = min(f[i+1][i], f[i][j] + dist(j, i+1));
f[i+1][j] = min(f[i+1][j], f[i][j] + dist(i, i+1));
其实, "向右走" 就是一个天然的"序". 这就可以让该dp满足"无后效性"原则
这也就是TSP不能用这种方法的原因.
为何我们不会漏掉可能的情况?
思考一下, 是不是每一条走完{1..n}的路线都存在一个走完{1..i}(i<n)的子路线? 所以不会漏.
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(_i, _st, _ed) for(int _i = (_st); _i <= (_ed); ++_i)
#define per(_i, _ed, _st) for(int _i = (_ed); _i >= (_st); --_i)
inline int read(){int ans = 0, f = 1; char c = getchar();while(c < '0' || c > '9') f = (c == '-') ? -1 : f, c = getchar();while('0' <= c && c <= '9') ans = ans*10 + c - '0', c = getchar();return ans;}
const int maxn = 1005;
double f[maxn][maxn];
struct poi{
double x, y;
bool operator < (const poi &rhs) const{
return x < rhs.x;
}
}p[maxn];
int n;
#define sqr(_x) ((_x)*(_x))
double dist(int a, int b){
return sqrt(sqr(p[a].x-p[b].x) + sqr(p[a].y - p[b].y));
}
signed main(){
while(cin >> n) {
rep(i, 1, n) cin >> p[i].x >> p[i].y;
if(n == 1) {
puts("0.00");
continue;
}
sort(p+1, p+n+1);
rep(i, 1, n) rep(j, 1, n) f[i][j] = 1e10;
//i > j
f[2][1] = dist(1, 2);
rep(i, 2, n) rep(j, 1, i-1){
f[i+1][i] = min(f[i+1][i], f[i][j] + dist(j, i+1));
f[i+1][j] = min(f[i+1][j], f[i][j] + dist(i, i+1));
}
printf("%.2f\n", f[n][n-1] + dist(n, n-1));
}
return 0;
}
Chapter_9 DP : uva1347 tour (bitonic tour)的更多相关文章
- POJ2677 Tour[DP 状态规定]
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4307 Accepted: 1894 Description ...
- POJ2677 Tour(DP+双调欧几里得旅行商问题)
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3929 Accepted: 1761 Description ...
- F - Free DIY Tour(动态规划,搜索也行)
这道题可用动态规划也可以用搜索,下面都写一下 Description Weiwei is a software engineer of ShiningSoft. He has just excelle ...
- 动态规划:HDU1224-Free DIY Tour
Free DIY Tour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- SPOJ:Free tour II (树分治+启发式合并)
After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...
- 【SPOJ】1825. Free tour II(点分治)
http://www.spoj.com/problems/FTOUR2/ 先前看了一会题解就自己yy出来了...对拍过后交tle.................. 自己造了下大数据........t ...
- HDU 1224 Free DIY Tour(spfa求最长路+路径输出)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1224 Free DIY Tour Time Limit: 2000/1000 MS (Java/Oth ...
- spoj 1825 Free tour II
http://www.spoj.com/problems/FTOUR2/ After the success of 2nd anniversary (take a look at problem FT ...
- SPOJ1825 FTOUR2 - Free tour II
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
随机推荐
- vscode设置python3.7调试环境(已更新)
汇总系列:https://www.cnblogs.com/dunitian/p/4822808.html#ai CentOS安装Python3.7:https://www.cnblogs.com/do ...
- Java斗地主案例、异常和自定义异常整理
模拟斗地主洗牌发牌 1.1 案例介绍 按照斗地主的规则,完成洗牌发牌的动作. 具体规则: 1. 组装54张扑克牌 2. 将54张牌顺序打乱 3. 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张 ...
- 使用docker部署SqlServer
踩了很多坑,来记录一下 首先说sqlserver 1. 安装docker要使用centos 7以上版本,使用centos 6及以下版本会出现各种问题 2. docker CE安装过程 $ sudo y ...
- 同步Name到Comment 及 同步 Comment 到Name
在 PowerDesigner执行命令 Tools->Execute Commands->Edit/Run Scripts 代码一:将Name中的字符COPY至Comment中 Opti ...
- python3 asyncio-协程模块测试代码
import time import asyncio #统计运行时间的装饰器 def run_time(func): def wrapperfunc(*argv, **kwargv): now = l ...
- 时间序列函数resamlpe详解
resample与groupby的区别:resample:在给定的时间单位内重取样groupby:对给定的数据条目进行统计 函数原型:DataFrame.resample(rule, how=None ...
- 出现Failed to get convolution algorithm的解决方法
当运行卷积神经时出现了问题:Failed to get convolution algorithm. This is probably because cuDNN failed to initiali ...
- Java中谈尾递归--尾递归和垃圾回收的比较
一.首先我们讲讲递归 1.递归的本质是,某个方法中调用了自身,本质还是调用了一个方法,只是这个方法正好是自身而已 2.递归因为是在自身中调用自身,所以会带来以下三个显著特点: 1.调用的是同一个 ...
- rest_framework框架的认识
它是基于Django的,帮助我们快速开发符合RESTful规范的接口框架. 一 路由 可以通过路由as_view()传参 根据请求方式的不同执行对应不同的方法 在routers模块下 封装了很多关于 ...
- Java基础8-多线程;同步代码块
作业解析 利用白富美接口案例,土豪征婚使用匿名内部类对象实现. interface White{ public void white(); } interface Rich{ public void ...