Advertisement
juaniisuar

Untitled

Apr 29th, 2015
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <stack>
  6. #include <cmath>
  7. #define forn(i,n) for((i)=0;(i)<(n);(i)++)
  8. #define MAXN 500
  9.  
  10. using namespace std;
  11.  
  12. int N,S,ROOT[MAXN];
  13.  
  14. struct edge{
  15. int n1,n2;
  16. float d;
  17. };
  18.  
  19. struct point{
  20. int x,y;
  21. };
  22.  
  23. bool operator < (const edge a, const edge b){
  24. return a.d>b.d;
  25. }
  26.  
  27. vector <point> NODO;
  28. priority_queue <edge> ARISTA;
  29. stack <float> Q;
  30.  
  31. int findSet (int n){
  32. if(ROOT[n]==n)
  33. return n;
  34. return ROOT[n]=findSet(ROOT[n]);
  35. }
  36.  
  37. bool isSameSet (int a, int b){
  38. return findSet(a)==findSet(b);
  39. }
  40.  
  41. void unionSet (int a, int b){
  42. ROOT[findSet(b)]=ROOT[a];
  43. }
  44.  
  45. float distancia (int a, int b){
  46. int x=NODO[a].x-NODO[b].x;
  47. int y=NODO[a].y-NODO[b].y;
  48. return sqrt(x*x+y*y);
  49. }
  50.  
  51. int main()
  52. {
  53. int testcases,kappa;
  54. int i,j,k;
  55. scanf("%i",&testcases);
  56. forn(kappa,testcases){
  57. scanf("%i %i",&S,&N);
  58. point naux;
  59. edge aaux;
  60. forn(i,N){
  61. scanf("%i %i",&j,&k);
  62. naux.x=j; naux.y=k;
  63. NODO.push_back(naux);
  64. }
  65. forn(i,N){
  66. for(j=i+1;j<N;j++){
  67. aaux.d=distancia(i,j); aaux.n1=i; aaux.n2=j;
  68. ARISTA.push(aaux);
  69. }
  70. }
  71.  
  72. forn(i,N)
  73. ROOT[i]=i;
  74. while(!ARISTA.empty()){
  75. aaux=ARISTA.top();
  76. ARISTA.pop();
  77. cout<<aaux.n1<<"\t"<<aaux.n2<<"\t"<<aaux.d<<endl;
  78. if(!isSameSet(aaux.n1,aaux.n2)){
  79. //cout<<aaux.n1<<"\t"<<aaux.n2<<"\t"<<aaux.d<<endl;
  80. unionSet(aaux.n1,aaux.n2);
  81. Q.push(aaux.d);
  82. }
  83. }
  84.  
  85. S /= 2;
  86. while(!Q.empty() && S){
  87. Q.pop();
  88. S--;
  89. }
  90. if(Q.empty())
  91. printf("%.2f\n", 0);
  92. else
  93. printf("%.2f\n",Q.top());
  94. while(!Q.empty())
  95. Q.pop();
  96.  
  97. }
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement