九度OJ 1144:Freckles (最小生成树)

题目描述:

    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. 

输入:

    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.

输出:

    Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

样例输入:
3
1.0 1.0
2.0 2.0
2.0 4.0
样例输出:
3.41
来源:

2009年北京大学计算机研究生机试真题

好久没有写过这么有成就感的代码了,激动之余竟然忘了怎么使用qsort()函数了

Is there any library function available in C standard library to do sort?

qsort() is the function you're looking for. You call it with a pointer to your array of data, the number of elements in that array, the size of each element and a comparison function.

It does its magic and your array is sorted in-place. An example follows:

#include <stdio.h>
#include <stdlib.h>
int comp (const void * elem1, const void * elem2) 
{
    int f = *((int*)elem1);
    int s = *((int*)elem2);
    if (f > s) return  1;
    if (f < s) return -1;
    return 0;
}
int main(int argc, char* argv[]) 
{
    int x[] = {4,5,2,3,1,0,9,8,6,7};

    qsort (x, sizeof(x)/sizeof(*x), sizeof(*x), comp);

    for (int i = 0 ; i < 10 ; i++)
        printf ("%d ", x[i]);

    return 0;
}

当然,破题的关键还是怎么运用到结构体,感谢万能的google

C/C++ standard library <stdlib.h> contains qsort function.

This is not the best quick sort implementation in the world but it fast enough and VERY EASY to be used... the formal syntax of qsort is:

qsort(<arrayname>,<size>,sizeof(<elementsize>),compare_function);

The only thing that you need to implement is the compare_function, which takes in two arguments of type "const void", which can be cast to appropriate data structure, and then return one of these three values:

  • negative, if a should be before b
  • 0, if a equal to b
  • positive, if a should be after b

1. Comparing a list of integers:

simply cast a and b to integers if x < y,x-y is negative, x == yx-y = 0x > yx-y is positive x-y is a shortcut way to do it :) reverse *x - *y to *y - *x for sorting in decreasing/reverse order

int compare_function(const void *a,const void *b) {
int *x = (int *) a;
int *y = (int *) b;
return *x - *y;
}

2. Comparing a list of strings:

For comparing string, you need strcmp function inside <string.h> lib. strcmp will by default return -ve,0,ve appropriately... to sort in reverse order, just reverse the sign returned by strcmp

#include <string.h>
int compare_function(const void *a,const void *b) {
return (strcmp((char *)a,(char *)b));
}

3. Comparing floating point numbers:

int compare_function(const void *a,const void *b) {
double *x = (double *) a;
double *y = (double *) b;
// return *x - *y; // this is WRONG...
if (*x < *y) return -1;
else if (*x > *y) return 1; return 0;
}

4. Comparing records based on a key:

Sometimes you need to sort a more complex stuffs, such as record. Here is the simplest way to do it using qsort library.

typedef struct {
int key;
double value;
} the_record;

int compare_function(const void *a,const void *b) {
the_record *x = (the_record *) a;
the_record *y = (the_record *) b;
return x->key - y->key;
}
废话不多说,用好并查集,省时又省力
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
# define N 100
int Tree[N];

typedef struct{
	double x,y;
	int position;
} point;

typedef struct{
	point p1,p2;
	double length;
} edge;
int comp(const void * a, const void * b){
	edge *c = (edge *) a;
	edge *d = (edge *) b; 
	return (c->length) > (d->length) ? 1 : -1;
}
int findRoot(int x){
	int temp;
	if(Tree[x] == -1)
		return x;
	temp = findRoot(Tree[x]);
	Tree[x] = temp;
	return temp;
}
double compute(point p1,point p2){
	return sqrt( pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2) );
}
int main(){
	int r1,r2,m,n,i,j,k,times;
	double ans;
	point vilage[101];
	edge edges[500];
	scanf("%d",&n);
	for(i=1; i<=n; i++){
		scanf("%lf%lf",&(vilage[i].x),&(vilage[i].y));
		vilage[i].position = i;
	}
	k = 1;
	for(i=1; i<=n;i++){
		for(j=1; j<=n; j++){
			if(i < j){
				edges[k].p1 = vilage[i];
				edges[k].p2 = vilage[j];
				edges[k].length = compute(vilage[i], vilage[j]);
				k++;
			}
		}
	}
	qsort(edges+1,k-1,sizeof(edge),comp);
	for(i=1; i<=n; i++){
		Tree[i] = -1;
	}
	ans = 0;
	for(i=1; i<k; i++){
		r1 = edges[i].p1.position;
		r2 = edges[i].p2.position;
		r1 = findRoot(r1);
		r2 = findRoot(r2);
		if(r1 != r2){
			ans += edges[i].length;
			Tree[r1] = r2;
		}
	}
	printf("%.2f\n",ans);
}

猜你喜欢

转载自blog.csdn.net/u013862444/article/details/80989646