DSA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.7 KiB

  1. /*
  2. #pragma GCC target ("avx2")
  3. #pragma GCC optimize ("Ofast")
  4. #pragma GCC optimize ("unroll-loops")
  5. */
  6. #include "bits/stdc++.h"
  7. #define iosb ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
  8. #define BIT(x) __builtin_popcount(x)
  9. #define all(x) x.begin() , x.end()
  10. #define F first
  11. #define S second
  12. #define pb push_back
  13. using namespace std ;
  14. typedef unsigned long long UL ;
  15. typedef long long L ;
  16. typedef string T ;
  17. typedef int I ;
  18. const I MaxN = 1e5+1 ;
  19. const I MOD = 1e9+7 ;
  20. const float INF = 1e9+7 ;
  21. pair<int,int> a[5001] ;
  22. int p[5001] ;
  23. float g[5001][5001], cost ;
  24. int32_t main()
  25. {
  26. freopen("unionday.in" , "r" , stdin) ;
  27. freopen("unionday.out" , "w" , stdout) ;
  28. int n ;
  29. cin >> n ;
  30. for(int i = 0 ; i < n ; ++i)
  31. {
  32. cin >> a[i].F >> a[i].S ;
  33. for(int j = 0 ; j < i ; ++j)
  34. {
  35. float d = (a[i].F-a[j].F)*(a[i].F-a[j].F)+(a[i].S-a[j].S)*(a[i].S-a[j].S) ;
  36. d = sqrt(d) ;
  37. g[i][j] = d ;
  38. g[j][i] = d ;
  39. }
  40. }
  41. vector<bool> used (n);
  42. vector<float> min_e (n, INF);
  43. vector<int> sel_e (n, -1);
  44. min_e[0] = 0;
  45. for (int i=0; i<n; ++i) {
  46. int v = -1;
  47. for (int j=0; j<n; ++j)
  48. if (!used[j] && (v == -1 || min_e[j] < min_e[v]))
  49. v = j;
  50. if (min_e[v] == INF) {
  51. cout << "No MST!";
  52. exit(0);
  53. }
  54. used[v] = true;
  55. if (sel_e[v] != -1)
  56. cost += g[v][sel_e[v]];
  57. for (int to=0; to<n; ++to)
  58. if (g[v][to] < min_e[to]) {
  59. min_e[to] = g[v][to];
  60. sel_e[to] = v;
  61. }
  62. }
  63. cout.precision(4) ;
  64. cout << fixed << cost << '\n' ;
  65. }