旅游

【题目链接】旅游

【题目类型】DP

&题解:

紫书P269 代码很简单,但思路很难。很难能想到要把一个圈分成2条线段,很难想到d(i,j)表示的是已经走过max(i,j)还需要的距离值,当然设d为还需要的距离值,这很常见。

还有也很难想到下一步只能走到i+1。

【时间复杂度】O(n^2)

&代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d %d",&(N),&(M))
#define SIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define rep(i,b) for(int i=0;i<(b);i++)
#define rez(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
const ll LINF = 0x3f3f3f3f3f3f3f3f;
#define PU(x) puts(#x);
#define PI(A) cout<<(A)<<endl;
#define DG(x) cout<<#x<<"="<<(x)<<endl;
#define DGG(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl;
#define DGGG(x,y,z) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl;
#define PIar(a,n) rep(i,n)cout<<a[i]<<" ";cout<<endl;
#define PIarr(a,n,m) rep(aa,n){rep(bb, m)cout<<a[aa][bb]<<" ";cout<<endl;}
const double EPS = 1e-9 ;
/* //////////////////////// C o d i n g S p a c e //////////////////////// */
const int MAXN = 1000 + 9 ;
struct pnt{
int x,y;
}p[MAXN];
int n;
double d[MAXN][MAXN];
double dis(int i,int j){
return hypot(p[i].x-p[j].x,p[i].y-p[j].y);
}
double dp(int i,int j){
double& ans=d[i][j];
if (i==n-1) return ans=dis(i,n)+dis(j,n);
if (ans>0) return ans;
ans=min(dp(i+1,j)+dis(i,i+1),dp(i+1,i)+dis(j,i+1));
return ans;
}
void Solve()
{
while(~SI(n)){
// dp problem input must start with 1
rez(i,1,n) SII(p[i].x,p[i].y);
cle(d,0);
double ans=dp(1,2)+dis(1,2);
printf("%.2lf\n", ans);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out","w",stdout);
#endif
//iostream::sync_with_stdio(false);
//cin.tie(0), cout.tie(0);
// int T;cin>>T;while(T--)
Solve();
return 0;
}

UVA1347 旅游(二维递归DP)的更多相关文章

  1. Vijos1392拼拼图的小衫[背包DP|二维信息DP]

    背景 小杉的幻想来到了经典日剧<死亡拼图>的场景里……被歹徒威胁,他正在寻找拼图(-.-干嘛幻想这么郁闷的场景……). 突然广播又响了起来,歹徒竟然又有了新的指示. 小杉身为新一代的汤浅, ...

  2. 棋盘分割(二维区间DP)

    题目大意:给一个棋盘,棋盘上每个格子中都有一个值,现在需要将棋盘切成n个矩形,总共切n-1刀,求最小的均方差.均方差定义为:,其中. 题目分析:将均方差化简得到:均方差2=(Σxi2)/n-平均值2. ...

  3. POJ 1661 Help Jimmy(二维DP)

    题目链接:http://poj.org/problem?id=1661 题目大意: 如图包括多个长度和高度各不相同的平台.地面是最低的平台,高度为零,长度无限. Jimmy老鼠在时刻0从高于所有平台的 ...

  4. Regionals 2014 >> Asia - Taichung 7003 - A Balance Game on Trees 树形DP + 二维费用背包

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. dp --- 二维dp + 最大上升子序列

    <传送门> 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 74477   Accepted: 27574 ...

  6. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  7. 二维背包 hdu2159

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 题目里面有两个限制条件,忍耐度和杀怪数量,所以可以用一个二维数组dp[i][j]来表示在消耗忍耐 ...

  8. 动态规划:HDU3496-Watch The Movie(二维费用的背包问题)

    Watch The Movie Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  9. NYOJ - 括号匹配(二)(经典dp)

    括号匹配(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描写叙述 给你一个字符串,里面仅仅包括"(",")","[&quo ...

随机推荐

  1. Codeforces Round #138 (Div. 2)

    A. Parallelepiped 枚举其中一边,计算其他两条边. B. Array 模拟. C. Bracket Sequence 栈. D. Two Strings \(pre[i]\)表示第i个 ...

  2. hive学习笔记_hive的表创建

    创建hive表注意事项 一.表分隔符必须与读取的数据文件一致,比如例子的分隔符为 '\t'(制表符),hive下默认分隔符是制表符. 二.最好指定分区作为数据之间的区分. 三.创建完表可以desc+表 ...

  3. ExtJS布局方式(layout)图文详解

    Auto默认布局 不给下级组件指定大小和位置 Absolute绝对布局 可使用坐标(x.y)进行布局 Accordion手风琴布局 实现Accordion效果的布局,也可叫可折叠布局.也就是说使用该布 ...

  4. Questions that are independent of programming language. These questions are typically more abstract than other categories.

    Questions that are independent of programming language.  These questions are typically more abstract ...

  5. tomcat集群待整理

    对于tomcat的集群有两种方式,这个主要是针对 session而言的.一种就是sticky模式,即黏性会话模式:另外一种就是session复制模式了.所谓sticky模式就是说同一个用户的访问 请求 ...

  6. PostgreSQL: Query for location of global tablespace?

    Q: I have been trying to make our database clients pro-active about not filling up the partition on ...

  7. java 锁讲解

    每个锁对象都有两个队列,一个是就绪队列,一个是阻塞队列,就绪队列存储了将要获得锁的线程,阻塞队列存储了被阻塞的线程,当一个被线程被唤醒 (notify)后,才会进入到就绪队列,等待获得锁.当一开始线程 ...

  8. C#学习之LinqtoSql类的简单例子

    LinqtoSql类把访问.操作数据库的细节封装了起来,把连接操作数据库变得相当简单.下面是简单的例子. 第一步:添加LinqtoSql类 1.创建一个控制台应用程序项目,下载一个NrothWind ...

  9. PHP正则匹配邮件地址、URL

    匹配邮件 #^[A-Za-z0-9]+[\w\.-]*@[A-Za-z0-9]+[A-Za-z0-9\.-]*[A-Za-z0-9]$# 注意 1. \w 表示的是 [A-Za-z0-9_] 包括下划 ...

  10. linux库之pam

    http://www.ibm.com/developerworks/cn/linux/l-pam/http://docs.oracle.com/cd/E19253-01/819-7056/ch3pam ...