Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cmath>
- #include <algorithm>
- #include <vector>
- #include <iomanip>
- using namespace std;
- ifstream f("desen.in");
- ofstream g("desen.out");
- class Punct {
- public:
- int x, y;
- double dist(Punct P) {
- return sqrt( (double) (x - P.x) * (x - P.x) + (y - P.y) * (y - P.y) );
- }
- friend istream& operator >> (istream& is, Punct &P) {
- is >> P.x >> P.y;
- return is;
- }
- };
- int N, i, j;
- Punct Puncte[1001];
- int uf[1001], H[1001];
- double sol;
- class Muchie {
- public:
- int x, y;
- double cost;
- Muchie(int p1, int p2) {
- x = p1;
- y = p2;
- cost = Puncte[x].dist(Puncte[y]);
- }
- bool operator < (Muchie M) const {
- return cost < M.cost;
- }
- };
- int Find(int x) {
- if (x == uf[x]) {
- return x;
- }
- int root = Find(uf[x]);
- uf[x] = root;
- return root;
- }
- void Union (int x, int y) {
- x = Find(x);
- y = Find(y);
- if (H[x] < H[y]) {
- uf[x] = y;
- }
- else if (H[x] > H[y]) {
- uf[y] = x;
- }
- else {
- uf[x] = y;
- H[y] ++;
- }
- }
- double Kruskal (int N, vector <Muchie> &M, vector <Muchie> &B) {
- double res = 0;
- int i;
- for (i = 1; i <= N; ++i) {
- uf[i] = i;
- H[i] = 1;
- }
- sort(M.begin(), M.end());
- int k = 0;
- for (auto v : M) {
- if (Find(v.x) != Find(v.y)) {
- Union(v.x, v.y);
- B.push_back(v);
- res += v.cost;
- ++k;
- }
- if (k == N - 1)
- return res;
- }
- return -1;
- }
- vector <Muchie> A, B;
- int main() {
- f >> N;
- for (i = 1; i <= N; ++i)
- f >> Puncte[i];
- g << fixed << setprecision(6) << 0.0 << '\n';
- for (i = 2; i <= N; ++i) {
- for (j = 1; j < i; ++j)
- A.push_back( Muchie(i, j) );
- sol = Kruskal (i, A, B);
- A = B;
- B.clear();
- g << fixed << setprecision(6) << sol << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement