Morass

Freckles

Jul 24th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cmath>
  3. #include <queue>
  4. using namespace std;
  5. int N;
  6. double vert[128][128],x[128],y[128];
  7. bool cn[128];
  8. struct pr{
  9.     pr(double f,int s):first(f),second(s){};
  10.     double first;
  11.     int second;
  12.     bool operator<(const pr &o)const{return first>o.first;}
  13. };
  14. inline double dist(double x1,double y1,double x2,double y2){
  15.     double X(x2-x1),Y(y1-y2);
  16.     return sqrt(X*X+Y*Y);
  17. }
  18. double solve(void){
  19.     priority_queue<pr> q;
  20.     q.push(pr(0,0));
  21.     double tot(0);
  22.     while(!q.empty()){
  23.         tot+=q.top().first;
  24.         int act(q.top().second);
  25.         q.pop();
  26.         cn[act]=true;
  27.         for(int i(0);i<N;++i)
  28.             q.push(pr(vert[act][i],i));
  29.         while(!q.empty()&&cn[q.top().second])
  30.             q.pop();
  31.     }
  32.     return tot;
  33. }
  34. int main(void){
  35.     int tt;
  36.     scanf("%d",&tt);
  37.     for(int g(0);tt--;++g){
  38.         if(g)
  39.             printf("\n");
  40.         scanf("%d",&N);
  41.         for(int i(0);i<N;++i){
  42.             cn[i]=false;
  43.             scanf("%lf%lf",&x[i],&y[i]);
  44.             vert[i][i]=0;
  45.         }
  46.         for(int i(0);i<N;++i)
  47.             for(int j(i+1);j<N;++j)
  48.                 vert[i][j]=vert[j][i]=dist(x[i],y[i],x[j],y[j]);
  49.         printf("%.2f\n",solve());
  50.     }
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment