Created
February 16, 2011 09:07
-
-
Save kitayuta/829074 to your computer and use it in GitHub Desktop.
POJ2560 通らない
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<cstdio> | |
#include<vector> | |
#include<algorithm> | |
#include<cmath> | |
using namespace std; | |
vector<pair<double,pair<int,int> > > edges; | |
vector<int> parent; | |
int find(int a){ | |
if(parent[a]==a)return a; | |
else return parent[a]=find(parent[a]); | |
} | |
void unite(int a,int b){ | |
int pa=find(a),pb=find(b); | |
if(pa!=pb)parent[pa]=pb; | |
} | |
int main(){ | |
int n; | |
scanf("%d",&n); | |
vector<pair<double,double> > ps(n); | |
parent=vector<int>(n); | |
for(int i=0;i<n;i++){ | |
double x,y; | |
scanf("%lf%lf",&x,&y); | |
ps[i]=make_pair(x,y); | |
parent[i]=i; | |
} | |
edges=vector<pair<double,pair<int,int> > >(); | |
for(int i=0;i<n;i++){ | |
for(int j=i+1;j<n;j++){ | |
edges.push_back(make_pair(pow(ps[i].first-ps[j].first,2)+pow(ps[i].second-ps[j].second,2),make_pair(i,j))); | |
} | |
} | |
sort(edges.begin(),edges.end()); | |
double ans=0; | |
for(int i=0;i<edges.size();i++){ | |
double le=edges[i].first; | |
int a=edges[i].second.first,b=edges[i].second.second; | |
if(find(a)!=find(b)){ | |
unite(a,b); | |
ans+=sqrt(le); | |
} | |
} | |
printf("%.2lf\n",ans); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment