Advertisement
double_trouble

Untitled

Apr 14th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 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; i++)
  60. for (int j = 0; j < n; j++)
  61. if (abs(i - j) > 2)
  62. dp[i][j] = inf;
  63.  
  64. for (int i = 0; i < n - 3; i++)
  65. {
  66. dp[i][i + 3] = min(dist(p[i], p[i + 2]), dist(p[i + 1], p[i + 3]));
  67. for (int j = 4; i + j < n; j++)
  68. {
  69. for (int k = i + 1; k < i + j; k++)
  70. 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]));
  71. }
  72. }
  73.  
  74. /*for (int i = 0; i < n; i++)
  75. {
  76. for (int j = 0; j < n; j++)
  77. cout << dp[i][j] << " ";
  78. cout << endl;
  79. }*/
  80. cout << fixed;
  81. cout << setprecision(10) << dp[0][n - 1] << endl;
  82.  
  83. //system("PAUSE");
  84.  
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement