luogu P1265 公路修建 最小生成树

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<algorithm>
using namespace std;
#define x first
#define y second
typedef pair<int,int>pii;
const int N=5050;
const int INF=1e9;
pii p[N];
double dist[N];
bool vis[N];
int n;
double calc(int x1,int y1,int x2,int y2)
{
	return sqrt((double)(x1-x2)*(x1-x2)+(double)(y1-y2)*(y1-y2));
}
int main()
{
	cin>>n;
	for(int i=1; i<=n; i++)
		cin>>p[i].x>>p[i].y;
	for(int i=2; i<=n; i++)
		dist[i]=INF;
	double ans=0;
	for(int i=1; i<=n; i++)
	{
		double miv=INF;
		int mipoj;
		for(int j=1; j<=n; j++)
			if(!vis[j]&&dist[j]<miv)
			{
				mipoj=j;
				miv=dist[j];
			}
		ans+=miv;
		vis[mipoj]=1;
		for(int j=1; j<=n; j++)
		{
			double t=calc(p[mipoj].x,p[mipoj].y,p[j].x,p[j].y);
			if(t<dist[j])
				dist[j]=t;
		}
	}
	printf("%.2lf
",ans);
	return 0;
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12882476.html