一开始看到给的坐标直接懵了,也没想出来怎么处理。
枚举所有符合条件的长度,个数在n^2之内
然后人工加上一个序号就好了~
1 #include "iostream" 2 #include "algorithm" 3 #include "cstdio" 4 #include "cmath" 5 using namespace std; 6 #define MAXN 111 7 8 struct loc{ 9 double a,b; 10 }q[MAXN]; 11 12 struct node{ 13 int x,y; 14 double l; 15 int n; 16 }p[MAXN*MAXN]; 17 18 int fa[MAXN*MAXN]; 19 int n, num; 20 double ans; 21 bool ok; 22 bool cmp(node x,node y) 23 { 24 return x.l < y.l; 25 } 26 27 void init() 28 { 29 num = 0; 30 ans = 0; 31 for (int i = 0;i < MAXN*MAXN; ++ i) 32 fa[i] = i,p[i].n = 1; 33 } 34 35 int find(int x) 36 { 37 return fa[x] = fa[x] == x ? x:find(fa[x]); 38 } 39 40 void process() 41 { 42 init(); 43 cin >> n; 44 for (int i = 0;i < n ; ++ i) 45 cin >> q[i].a >> q[i].b; 46 for (int i = 0;i < n; ++ i) 47 for (int j = i+1;j < n; ++ j) { 48 p[num].x = i; 49 p[num].y = j; 50 double len = sqrt((q[i].a - q[j].a)*(q[i].a - q[j].a) + (q[i].b - q[j].b)*(q[i].b- q[j].b)); 51 if (len >= 10 && len <= 1000) { 52 p[num].l = len; 53 num++; 54 } 55 } 56 sort(p,p+num,cmp); 57 for (int i = 0; i < num ; ++ i) { 58 int x = find(p[i].x); 59 int y = find(p[i].y); 60 if (x != y) { 61 fa[y] = x; 62 ans += p[i].l*100; 63 p[x].n += p[y].n; 64 } 65 } 66 int max = -1; 67 for (int i = 0;i < num; ++ i) 68 if (p[i].n > max) max = p[i].n; 69 if (max == n) printf("%.1lf ",ans); 70 else cout << "oh!" << endl; 71 } 72 73 int main() 74 { 75 int T; 76 cin >> T; 77 while (T--) { 78 process(); 79 } 80 return 0; 81 }