HDU 1875, swust acm ACM team 2016 - minimum spanning tree A - unblocked Project Renewal

Keywords: less

Believe you all heard of a place called "Baidao Lake". Baidao Lake residents live on different islands. When they want to go to other islands, they have to do so by boating. Now the government has decided to vigorously develop Baidao Lake. The first problem to be solved is of course the traffic problem. The government has decided to achieve the full unimpeded development of Baidao Lake. After the investigation team RPRush fully understood the situation of Baidao Lake, he decided to build a bridge between the eligible islands. The so-called eligible islands is that the distance between the two islands can not be less than 10 meters, nor greater than 1000 meters. Of course, in order to save money, only two islands need to be accessible. The price of the bridge is 100 yuan/m. Input The input includes multiple sets of data. The input first includes an integer T (T <= 200), which represents a T group of data.
Each set of data is first an integer C (C <= 100), representing the number of islands, followed by C coordinates, representing the coordinates of each island, which are all integers of 0 <= x, y <= 1000.
Output Each group of input data output a row, representing the minimum cost of building the bridge, the result retains a decimal. If the project can not be achieved to achieve full unimpeded, output "oh!". Sample Input
2
2
10 10
20 20
3
1 1
2 2
1000 1000
Sample Output
1414.2
oh!
#include<stdio.h>
#include<math.h>
#Include < stdlib.h >//qsort function header file 
int p[10000];
typedef struct
{
	int x;
	int y;
	double date;
}node;
node a[10000],b[10000];//a is used to store the coordinates of each point, b is used to store the distance between no two points and the ordinal number of those two points. 
int find(int x)
{
	return p[x]!=x?p[x]=find(p[x]):x;
}
int  he(int a,int b)
{
	if(find(a)==find(b))
	return 0;
	else
	{
		int x;
		int y;
		x=find(a);
		y=find(b);
		p[x]=find(y);
		return  1;
	}
	
}
int cmp( const void *a ,const void *b)//Part of qsort function 

{


node *aa = (node *)a;
node *bb = (node *)b;
return aa->date > bb->date ? 1 : -1;

}
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		node t;
		double sum=0;
		int count=0;
		int k=0;
		int i,j;
		int m;
		scanf("%d",&m);
		for(i=0;i<m;i++)
		p[i]=i;
		for(i=0;i<m;i++)
		scanf("%d %d",&a[i].x,&a[i].y);
		for(i=0;i<m;i++)
		for(j=0;j<m;j++)
		{
			b[k].x=i;
			b[k].y=j;
			b[k].date=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
			k++;
		}
	    qsort(b,k-1,sizeof(b[0]),cmp);
		for(i=0;i<k;i++)
		{
		
		if(b[i].date>=10&&b[i].date<=1000&&he(b[i].x,b[i].y))
		{
			
			count++;
			sum+=b[i].date;
					
		}
		if(count==m-1)//The number after the establishment is equal to the number of points - 1 
		break;
		
		}
		if(count==m-1)
		printf("%.1lf\n",sum*100);
		else
		printf("oh!\n");
	}
	return 0;
}

Posted by elecktricity on Tue, 12 Feb 2019 13:21:17 -0800