Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef long long ll;
- const int INF = 1000000000;
- struct pt
- {
- ll x, y;
- };
- double dist(const pt& a, const pt& b)
- {
- return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
- }
- int chosenV[5000];
- double minD[5000];
- char used[5000];
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.setf(cout.fixed);
- cout.precision(12);
- int n;
- cin >> n;
- vector<pt> pts(n);
- for (int i = 0; i < n; i++)
- {
- cin >> pts[i].x >> pts[i].y;
- }
- fill(chosenV, chosenV + n, -1);
- fill(minD, minD + n, INF);
- minD[0] = 0;
- double res = 0;
- for (int i = 0; i < n; i++)
- {
- int v = -1;
- for (int j = 0; j < n; j++)
- {
- if (!used[j] && (v == -1 || minD[v] > minD[j]))
- {
- v = j;
- }
- }
- used[v] = 1;
- if (chosenV[v] != -1)
- res += dist(pts[v], pts[chosenV[v]]);
- for (int j = 0; j < n; j++)
- {
- if (minD[j] > dist(pts[v], pts[j]))
- {
- minD[j] = dist(pts[v], pts[j]);
- chosenV[j] = v;
- }
- }
- }
- cout << res;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement