Minimum spanning tree K small edge problem.
Test instructions is said to have n points, there are two ways to connect, satellite and wireless, satellite free to connect, unlimited need to configure the receiver, the receiver price and acceptable distance is the same.
The satellite frequency is limited and has m channels.
That is to say, build the smallest spanning tree, front n-1-m side with wireless, behind M side with satellite.
Use an array to save the cost for each time, and the final output is good.
#include <cstdio> #include <cstring> #include <string> #include <queue> #include <algorithm > #include <queue> #include <map> #include <stack> #include <iostream> #include <list># include<set> #include <cmath> #define INF 0x7fffffff#define eps 1e-6using namespace std;int n,m;int fa[501]; struct lx{int u,v; Double Len;} L[501*250];struct node{double x, y;} Point[501];int father (int x) {if (x!=fa[x]) Fa[x]=father (fa[x]); return fa[x];} BOOL CMP (LX A,lx b) {return a.len<b.len;} int main () {int t; scanf ("%d", &t); while (t--) {scanf ("%d%d", &m,&n); for (int i=0;i<=n;i++) fa[i]=i; for (int i=1;i<=n;i++) scanf ("%lf%lf", &point[i].x,&point[i].y); int cot=0; for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) {l[cot].u=i; L[cot].v=j; L[cot++].len=sqrt (Pow (point[i].x-point[j].x,2) +pow (point[i].y-point[j].y,2)); } sort (l,l+cot,cmp); Double path[501]; int pcot=1; for (int i=0;i<cot;i++) {int fu=father (L[I].U); int Fv=father (L[I].V); if (FU==FV) continue; Fa[fv]=fu; Path[pcot++]=l[i].len; } printf ("%.2f\n", path[pcot-m]); }}