Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <queue>
- #include <stack>
- #include <cmath>
- #define forn(i,n) for((i)=0;(i)<(n);(i)++)
- #define MAXN 500
- using namespace std;
- int N,S,ROOT[MAXN];
- struct edge{
- int n1,n2;
- float d;
- };
- struct point{
- int x,y;
- };
- bool operator < (const edge a, const edge b){
- return a.d>b.d;
- }
- vector <point> NODO;
- priority_queue <edge> ARISTA;
- stack <float> Q;
- int findSet (int n){
- if(ROOT[n]==n)
- return n;
- return ROOT[n]=findSet(ROOT[n]);
- }
- bool isSameSet (int a, int b){
- return findSet(a)==findSet(b);
- }
- void unionSet (int a, int b){
- ROOT[findSet(b)]=ROOT[a];
- }
- float distancia (int a, int b){
- int x=NODO[a].x-NODO[b].x;
- int y=NODO[a].y-NODO[b].y;
- return sqrt(x*x+y*y);
- }
- int main()
- {
- int testcases,kappa;
- int i,j,k;
- scanf("%i",&testcases);
- forn(kappa,testcases){
- scanf("%i %i",&S,&N);
- point naux;
- edge aaux;
- forn(i,N){
- scanf("%i %i",&j,&k);
- naux.x=j; naux.y=k;
- NODO.push_back(naux);
- }
- forn(i,N){
- for(j=i+1;j<N;j++){
- aaux.d=distancia(i,j); aaux.n1=i; aaux.n2=j;
- ARISTA.push(aaux);
- }
- }
- forn(i,N)
- ROOT[i]=i;
- while(!ARISTA.empty()){
- aaux=ARISTA.top();
- ARISTA.pop();
- cout<<aaux.n1<<"\t"<<aaux.n2<<"\t"<<aaux.d<<endl;
- if(!isSameSet(aaux.n1,aaux.n2)){
- //cout<<aaux.n1<<"\t"<<aaux.n2<<"\t"<<aaux.d<<endl;
- unionSet(aaux.n1,aaux.n2);
- Q.push(aaux.d);
- }
- }
- S /= 2;
- while(!Q.empty() && S){
- Q.pop();
- S--;
- }
- if(Q.empty())
- printf("%.2f\n", 0);
- else
- printf("%.2f\n",Q.top());
- while(!Q.empty())
- Q.pop();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement