题解 | #Freckles#
Freckles
https://www.nowcoder.com/practice/41b14b4cd0e5448fb071743e504063cf
#include<cmath> #include <iostream> #include <algorithm> #include <vector> using namespace std; struct point{ float x,y; int loc; bool operator==(const point &p) { return (this->x == p.x) && (this->y == p.y); } }p; struct dist{ float dis; point id; }; float dis(point a,point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } bool cmp(dist a,dist b){ return a.dis<b.dis; } int main() { int c ; float res=0,a,b; cin>>c; vector<point> aa,bb; for (int i=0;i<c;i++){ point s; cin>>s.x>>s.y; aa.push_back(s); } bb.push_back(aa[0]); aa.erase(aa.begin()); vector<dist> di; while(aa.size()!=0){ int m=0; for(int i=0;i<aa.size();i++){ for(int j=0;j<bb.size();j++){ dist dd; dd.dis=dis(aa[i],bb[j]);dd.id=aa[i]; di.push_back(dd); m++; } } sort(di.begin(),di.begin()+m,cmp); res +=(*di.begin()).dis; dist p=di[0]; bb.insert(bb.begin(),p.id); for(vector<point>::iterator it=aa.begin();it!=aa.end();){ if(*it==p.id){aa.erase(it);} else{it++;} } di.clear(); } printf("%.2f", res); }