Eddy's picture + prime算法 + kruskal算法

本博客介绍了一种算法,用于计算在给定坐标点上绘制图纸时,如何通过直线连接所有点,形成闭合图形,并找出最短的总连线长度。通过输入点的数量和每个点的坐标,该算法能够输出最小总长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Eddy's picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5029    Accepted Submission(s): 2482


Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
 

Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

Input contains multiple test cases. Process to the end of file.
 

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

Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
 

Sample Output
3.41
 
/*思路:prime算法*/
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std ;
#define MAX 102 
#define inf 1000000000.0
typedef struct infor 
{
	double x , y ;
} infor ;
int n , vi[MAX] ;
infor a[MAX] ;
double s[MAX][MAX] ;
void prime()
{
	int i, j , k ;
	double sum = 0 , d[MAX] ;
	for( i = 0 ; i < n ; i ++)
		d[i] = s[0][i] ;
	d[0] = 0 ;
	vi[0] = 1;
	for( i = 1; i < n ; i ++)
	{
		double min1 = inf ;
		for( j = 0 ; j < n ; j ++)
			if(!vi[j] && min1 > d[j])
			{
				k = j ;
				min1 = d[j] ;
			}
		sum +=min1 ;
		if(sum >= inf )
			break;
		vi[k] = 1;
		for( j = 0 ; j < n ; j ++)
			if(!vi[j] && d[j] > s[k][j])
				d[j] = s[k][j] ;
	}
	cout <<fixed<<setprecision(2)<< sum << endl ;
}
int main(void)
{
	
	while( cin >> n )
	{
		memset(vi, 0 , sizeof(vi));
		int i , j ; 
		for( i = 0 ; i < n ; i ++ )
			cin >> a[i].x >> a[i].y ;
		for( i = 0 ; i < n ; i ++)
			for( j = 0 ; j < n ; j ++)
			{
				s[i][j] = s[j][i] = inf ; //初始化每两点的距离为无穷大
				double dis = 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 ));  //the distance between two points
				s[i][j] = s[j][i] = s[i][j] > dis ? dis : s[i][j] ; //aviod the same point,take the smallest one 
			}
			prime(); //begin prime algorithm 
	}
	return 0;
}
 
/*思路:kruskal算法*/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std ;
#define MAX 102
int father[MAX];
typedef struct infor
{
	int start , end ;
	double dis ;
}infor ;
infor s[MAX*MAX] ;
int n ;
bool cmp(infor a , infor b)
{
	if(a.dis < b.dis )
		return true ;
	else 
		return false ;
}
int findset(int x)
{
	return x!=father[x] ? father[x] = findset(father[x]): x;
}
double kruskal(int m)
{
	int i  , x , y;
	double mincost= 0 ;
	for( i = 0 ; i < m ; i ++)
	{
		x= s[i].start ;
		y = s[i].end ;
		x= findset(x);
		y = findset(y);
		if(x!=y)
		{
			father[x] = y ;
			mincost +=s[i].dis ;
		}
	}
	return mincost ;
}
int main(void)
{
	double mincost = 0 ;
	while(cin >> n )
	{
		int i , j ;
		double a[MAX] ,b[MAX];
		for( i = 0 ; i < n ; i ++)
			cin >> a[i] >> b[i] ;
		int num = 0 ;
		for( i = 0 ; i < n ; i ++)
			for( j = 0 ; j < n ; j ++)
			{
				double dis = sqrt((a[i] - a[j])*(a[i] - a[j]) + (b[i] - b[j]) * (b[i] - b[j] ));
				s[num].start = i ;
				s[num].end = j ;
				s[num++].dis = dis ;
			}
		for( i = 0 ; i < n ; i ++)
			father[i] = i ;
		sort(s, s + num , cmp);
		mincost = kruskal(num) ;
		cout<<fixed<<setprecision(2)<<mincost<<endl;
	
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值