Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cmath>
- #include <queue>
- using namespace std;
- int N;
- double vert[128][128],x[128],y[128];
- bool cn[128];
- struct pr{
- pr(double f,int s):first(f),second(s){};
- double first;
- int second;
- bool operator<(const pr &o)const{return first>o.first;}
- };
- inline double dist(double x1,double y1,double x2,double y2){
- double X(x2-x1),Y(y1-y2);
- return sqrt(X*X+Y*Y);
- }
- double solve(void){
- priority_queue<pr> q;
- q.push(pr(0,0));
- double tot(0);
- while(!q.empty()){
- tot+=q.top().first;
- int act(q.top().second);
- q.pop();
- cn[act]=true;
- for(int i(0);i<N;++i)
- q.push(pr(vert[act][i],i));
- while(!q.empty()&&cn[q.top().second])
- q.pop();
- }
- return tot;
- }
- int main(void){
- int tt;
- scanf("%d",&tt);
- for(int g(0);tt--;++g){
- if(g)
- printf("\n");
- scanf("%d",&N);
- for(int i(0);i<N;++i){
- cn[i]=false;
- scanf("%lf%lf",&x[i],&y[i]);
- vert[i][i]=0;
- }
- for(int i(0);i<N;++i)
- for(int j(i+1);j<N;++j)
- vert[i][j]=vert[j][i]=dist(x[i],y[i],x[j],y[j]);
- printf("%.2f\n",solve());
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment