旅游

【题目链接】旅游

【题目类型】DP

&题解:

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

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

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

&代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int INF = 0x3f3f3f3f;
  5. #define cle(a,val) memset(a,(val),sizeof(a))
  6. #define SI(N) scanf("%d",&(N))
  7. #define SII(N,M) scanf("%d %d",&(N),&(M))
  8. #define SIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
  9. #define rep(i,b) for(int i=0;i<(b);i++)
  10. #define rez(i,a,b) for(int i=(a);i<=(b);i++)
  11. #define red(i,a,b) for(int i=(a);i>=(b);i--)
  12. const ll LINF = 0x3f3f3f3f3f3f3f3f;
  13. #define PU(x) puts(#x);
  14. #define PI(A) cout<<(A)<<endl;
  15. #define DG(x) cout<<#x<<"="<<(x)<<endl;
  16. #define DGG(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl;
  17. #define DGGG(x,y,z) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl;
  18. #define PIar(a,n) rep(i,n)cout<<a[i]<<" ";cout<<endl;
  19. #define PIarr(a,n,m) rep(aa,n){rep(bb, m)cout<<a[aa][bb]<<" ";cout<<endl;}
  20. const double EPS = 1e-9 ;
  21. /* //////////////////////// C o d i n g S p a c e //////////////////////// */
  22. const int MAXN = 1000 + 9 ;
  23. struct pnt{
  24. int x,y;
  25. }p[MAXN];
  26. int n;
  27. double d[MAXN][MAXN];
  28. double dis(int i,int j){
  29. return hypot(p[i].x-p[j].x,p[i].y-p[j].y);
  30. }
  31. double dp(int i,int j){
  32. double& ans=d[i][j];
  33. if (i==n-1) return ans=dis(i,n)+dis(j,n);
  34. if (ans>0) return ans;
  35. ans=min(dp(i+1,j)+dis(i,i+1),dp(i+1,i)+dis(j,i+1));
  36. return ans;
  37. }
  38. void Solve()
  39. {
  40. while(~SI(n)){
  41. // dp problem input must start with 1
  42. rez(i,1,n) SII(p[i].x,p[i].y);
  43. cle(d,0);
  44. double ans=dp(1,2)+dis(1,2);
  45. printf("%.2lf\n", ans);
  46. }
  47. }
  48. int main()
  49. {
  50. #ifndef ONLINE_JUDGE
  51. freopen("1.in", "r", stdin);
  52. freopen("1.out","w",stdout);
  53. #endif
  54. //iostream::sync_with_stdio(false);
  55. //cin.tie(0), cout.tie(0);
  56. // int T;cin>>T;while(T--)
  57. Solve();
  58. return 0;
  59. }

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. poj1270 拓扑序(DFS)

    题意:给出将会出现的多个字母,并紧接着给出一部分字母的大小关系,要求按照字典序从小到大输出所有符合上述关系的排列. 拓扑序,由于需要输出所有排列,所以需要使用 dfs ,只要点从小到大遍历就可以实现字 ...

  2. 越狱Season 1- Episode 22: Flight

    Season 1, Episode 22: Flight -Franklin: You know you got a couple of foxes in your henhouse, right? ...

  3. 关于Lua程序设计{读书笔记}

    1.lua中的标识符可以是由任意字母.数字和下划线构成的字符串,但不能以数字开头.2.lua将通常类似"_VALUE"的标识符作为保留标识符3.lua的保留字 and break ...

  4. java的nio之:java的nio系列教程之SocketChannel

    Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道.可以通过以下2种方式创建SocketChannel: 打开一个SocketChannel并连接到互联网上的某台服务器. ...

  5. java多线程之:深入JVM锁机制2-Lock (转载)

    前文(深入JVM锁机制-synchronized)分析了JVM中的synchronized实现,本文继续分析JVM中的另一种锁Lock的实现.与synchronized不同的是,Lock完全用Java ...

  6. 无shell情况下的mysql远程mof提权利用方法详解

    扫到一个站的注入<ignore_js_op> 在havij中得到mysql数据库中mysql库保存的数据库密码:<ignore_js_op> 有时候发现1.15版的还是最好用, ...

  7. GDB动态库搜索路径

    当GDB无法显示so动态库的信息或者显示信息有误时,通常是由于库搜索路径错误导致的,可使用set sysroot.set solib-absolute-prefix.set solib-search- ...

  8. PHP- 深入PHP、Redis连接

    pconnect, phpredis中用于client连接server的api. The connection will not be closed on close or end of reques ...

  9. ASP.NET读取EXCEL文件的三种经典方法

      1.方法一:采用OleDB读取EXCEL文件:   把EXCEL文件当做一个数据源来进行数据的读取操作,实例如下: public DataSet ExcelToDS(string Path) { ...

  10. 013. asp.net统计网站访问人数

    Global.asax中的代码: <%@ Application Language="C#" %> <script runat="server" ...