문제

[UVa-10034] Freckles (Saving ink) 백준 4386

구코딩 2022. 10. 8. 01:07
728x90

Problem

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad’s back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley’s engagement falls through. Consider Dick’s back to be a plane with freckles at various (x, y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

 

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs. The first line contains 0 < n ≤ 100, the number of freckles on Dick’s back. For each freckle, a line follows; each following line contains two real numbers indicating the (x, y) coordinates of the freckle.

 

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

//Input_1.txt
3
1.0 1.0
2.0 2.0
2.0 4.0

Output_1.txt
3.41

 

//Input_2.txt
4
1.0 4.0
2.0 5.0
3.0 6.0
1.0 1.0

//Output_2.txt
5.83

 

//Input_3.txt
5
1.0 1.0
1.0 2.0
2.0 1.0
2.0 2.0
5.0 5.0

//Output_3.txt
7.24

 

Solution

#define _CRT_SECURE_NO_WARNINGS
#define NUMBER_OF_FILES 3
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct Edge {
    double d;
    int x, y;
}E;

int list[100];
double x[100];
double y[100];
E edge[5000];
int num = 0;

int compare(const void* a, const void* b) {
    E n1 = *(E*)a;
    E n2 = *(E*)b;
    if (n1.d > n2.d) {
        return 1;
    }
    else if (n1.d < n2.d) {
        return -1;
    }
    else
        return 0;
}

int find(int x) {
    if (list[x] < 0) {
        return x;
    }
    return list[x] = find(list[x]);
}

int Union(int x, int y) {
    x = find(x);
    y = find(y);

    if (x == y) {
        return 0;
    }

    if (list[x] < list[y]) {
        list[x] += list[y];
        list[y] = x;
        return list[x] * -1;
    }
    else {
        list[y] += list[x];
        list[x] = y;
        return list[y] * -1;
    }
}

int main() {
    char Input[3][15] = {
        "Input_1.txt", "Input_2.txt", "Input_3.txt"
    };
    char Output[3][15] = {
        "Output_1.txt", "Output_2.txt", "Output_3.txt"
    };
    for (int i = 0; i < NUMBER_OF_FILES; i++) {
        FILE* input;
        input = fopen(Input[i], "r");
        FILE* output;
        output = fopen(Output[i], "w");
        double result = 0, a, b;
        int size, u;
        int z = fscanf(input, "%d", &size);
        for (int i = 0; i < size; i++) {
            list[i] = -1;
            z = fscanf(input, "%lf %lf", &x[i], &y[i]);
            for (int j = 0; j < i; j++) {
                edge[num].x = i;
                edge[num].y = j;
                edge[num++].d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
            }
        }
        qsort(edge, num, sizeof(E), compare);
        for (int i = 0; i < num; i++) {
            u = Union(edge[i].x, edge[i].y);
            if (u) {
                result += edge[i].d;
                if (u == size) 
                    break;
            }
        }
        fprintf(output, "%.2lf", result);
   }
}

 

 

728x90
반응형