Advertisement
double_trouble

Untitled

Apr 14th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <string>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <set>
  9. #include <queue>
  10. #include <map>
  11.  
  12. using namespace std;
  13.  
  14. const long long inf = 2000000007;
  15.  
  16. struct point
  17. {
  18. double x, y;
  19. point(double x, double y) : x(x), y(y) {}
  20. };
  21.  
  22. point operator+(const point &a, const point &b)
  23. {
  24. return point(a.x + b.x, a.y + b.y);
  25. }
  26.  
  27. point operator-(const point &a, const point &b)
  28. {
  29. return point(a.x - b.x, a.y - b.y);
  30. }
  31.  
  32.  
  33. double dist(point a, point b)
  34. {
  35. return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  36. }
  37.  
  38. vector<point > p;
  39. double dp[1000][1000];
  40.  
  41. int main()
  42. {
  43. ios_base::sync_with_stdio(0);
  44. cin.tie(0);
  45. cout.tie(0);
  46.  
  47. //freopen("important.in", "r", stdin);freopen("important.out", "w", stdout);
  48.  
  49. int n;
  50. cin >> n;
  51.  
  52. double x, y;
  53. for (int i = 0; i < n; i++)
  54. {
  55. cin >> x >> y;
  56. p.push_back(point(x, y));
  57. }
  58.  
  59. for (int i = 0; i < n - 3; i++)
  60. {
  61. dp[i][i + 3] = min(dist(p[i], p[i + 2]), dist(p[i + 1], p[i + 3]));
  62. for (int j = 4; i + j < n; j++)
  63. {
  64. for (int k = i + 1; k < i + j; k++)
  65. dp[i][i + j] = min(dp[i][i + j], dp[i][k] + dp[k][i + j] + dist(p[i], p[k]) + dist(p[k], p[i+j]));
  66. }
  67. }
  68.  
  69. /*for (int i = 0; i < n; i++)
  70. {
  71. for (int j = 0; j < n; j++)
  72. cout << dp[i][j] << " ";
  73. cout << endl;
  74. }*/
  75. cout << fixed;
  76. cout << setprecision(10) << dp[0][n - 1] << endl;
  77.  
  78. //system("PAUSE");
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement