Luogu P1991 Wireless Communication Network

Keywords: Verilog

Topic portal

Ideas for solving problems:

For the first time, I looked at the question, a little confused, I do not know what this question is asking for. Then I looked at the algorithm label, the smallest spanning tree, and then I looked at the question again, as if that was the case.

This question is actually a board question of the smallest spanning tree. It says that we can install S satellite phones, and we know that it takes n-1 edges to connect n points into a tree. So in fact, what we need is the largest side of the n-1 edge. So we only need to run the smallest spanning tree once and know when we connect the s+1 edge. (Bengu uses Kruskal)

AC Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath> 
 5 
 6 using namespace std;
 7 
 8 int fa[100001],a[100001],b[100001],s,p,n,k;
 9 double ans;
10 struct kkk {
11     double x,y,z;
12 }e[1000001]; 
13 
14 bool cmp(kkk a,kkk b) {
15     return a.z < b.z;
16 }
17 
18 int find_father(int x) {
19     if(fa[x] == x) return x;
20     else return fa[x] = find_father(fa[x]);
21 }
22 
23 int main()
24 {
25     scanf("%d%d",&s,&p);
26     for(int i = 1;i <= p; i++) {
27         scanf("%d%d",&a[i],&b[i]);
28         for(int j = 1;j < i; j++) {
29             n++;
30             e[n].z = sqrt((a[i] - a[j]) * (a[i] - a[j]) + (b[i] - b[j]) * (b[i] - b[j]));
31             //Using Pythagorean Theorem to Find the Distance from Current Point to Other Points(You should all know the Pythagorean Theorem.) 
32             e[n].x = i;
33             e[n].y = j;
34         }
35     }
36     for(int i = 1;i <= p; i++) 
37         fa[i] = i;
38     sort(e+1,e+n+1,cmp);
39     for(int i = 1;i <= n; i++) {
40         int a = find_father(e[i].x); 
41         int b = find_father(e[i].y);
42         if(a != b) {
43             fa[a] = b;
44             ans = e[i].z;
45             k++;
46             if(k >= p - s) {
47                 printf("%.2lf",ans);
48                 return 0;
49             }
50         }
51     }
52     return 0;
53 }

Posted by iron999mike on Sat, 12 Oct 2019 13:53:27 -0700