https://blog.youkuaiyun.com/vocaloid01/article/details/76559746
别人随便一写,就是我半年,甚至一年的积累。什么时候能轻松地写出这么思路清晰的代码呀?
个人不足:
1,优先级队列的结构体用法
2,最小生成树简介版本
3,并查集
4,结构体 构造函数
5,重载运算符 operator
#include<bits/stdc++.h>
using namespace std;
double maps[105][2];
struct D
{
double len;
int head,tail;
D(int a,int b,double l)
{
head=a; tail=b;
len=l;
}
};
struct cmp
{
bool operator() (D a,D b)
{
return a.len>b.len;
}
};
int pre[105];
int find(int a)
{
if(pre[a]==a){return a;}
return pre[a]=find(pre[a]);
}
bool judge(int a,int b)
{
int A=find(a);
int B=find(b);
if(A!=B)
{
pre[A]=B; return 1;
}
else
return 0;
}
int main()
{
int n;
int key=1;
while(cin>>n && n!=0)
{
for(int i=0;i<n;i++)
cin>>maps[i][0]>>maps[i][1];
priority_queue<D ,vector<D>,cmp > Q;
double len;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
len=sqrt( pow(maps[i][0]-maps[j][0],2)+pow(maps[i][1]-maps[j][1],2) );
Q.push( D(i,j,len) );
}
for(int i=0;i<n;i++) pre[i]=i;
double length=0;
int edge=0;
while(edge<n-1)
{
D mid=Q.top();
Q.pop();
if(judge(mid.head,mid.tail ) )
{
edge++;
length+=mid.len;
}
}
printf("Case #%d:\n",key);
printf("The minimal distance is: %.2f\n\n",length);
key++;
}
return 0;
}