其他分享
首页 > 其他分享> > P1991无线通讯网

P1991无线通讯网

作者:互联网

一、题目描述

  

 

 二、解题思路

  首先预处理出路径长度,标记点。然后,因为只能装s个,所以答案就在前面p - s条路径中最长的那条,因为我后面那p - s + 1之后的只会导致答案变大,这里有点点贪心的思想,所以答案就只能是前面p - s中最大的那条就行了。

三、代码实现

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 double dis[510];
 4 int vis[510];
 5 struct node{
 6     int from,to;
 7     double w;
 8 }edges[251000];
 9 const double maxn = 1e9 + 10;
10 int s,p,cnt;
11 double ans = 1e9 + 10;
12 double a[510],b[510];
13 int c[510];
14 void init()
15 {
16     for(int i = 1;i <= p;i++)
17         c[i] = i;
18 }
19 int find(int u)
20 {
21     if(u == c[u])
22         return u;
23     return c[u] = find(c[u]);
24 }
25 bool cmp(node u,node v)
26 {
27     return u.w < v.w;
28 }
29 void kruscal()
30 {
31     int k = 0;
32     sort(edges + 1,edges + cnt + 1,cmp);
33     for(int i = 1;i <= cnt;i++){
34         int sx = find(edges[i].from);
35         int sy = find(edges[i].to);
36         if(sx !=  sy){
37             c[sx] = sy;
38             ans = edges[i].w;
39             k++;
40             if(k >= p - s){
41                 printf("%.2lf",ans);
42                 return ;
43             }
44         }
45     }
46     cout << ans;
47 }
48 int main()
49 {
50     cin >> s >> p;
51     init();
52     for(int i = 1;i <= p;i++){
53         double p1,p2;
54         cin >> a[i] >> b[i];
55         for(int j = 1;j < i;j++){
56             double kis = sqrt(pow(a[i] - a[j],2) + pow(b[i] - b[j],2));
57             // cout << kis << endl;
58             edges[++cnt].from = i;
59             edges[cnt].to = j;
60             edges[cnt].w = kis;
61         }
62     }
63     kruscal();
64     return 0;
65 }

标签:10,int,double,P1991,ans,1e9,510,无线通讯
来源: https://www.cnblogs.com/scannerkk/p/15898201.html