POJ 2349 Arctic Network(最下生成树+求第k大边)

题目链接:http://poj.org/problem?id=2349

题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置。 否则,只有两个前哨之间的距离不超过D,才能通过无线电进行通信。求出能使所有前哨都能直接或间接通信的最小的D。

解题思路:题目要求使所有前哨都能直接或间接通信,那么相当于使n个点相连,至少需要n-1条边。可以将n个点分为s个团,每个团内部时无限通信,团与团之间通过卫星通信。那么就相当于用s个卫星装置建立s-1条边,用无线电通信建立n-1-(s-1)==n-s条边,由于卫星通信是没有距离限制的那就可以选择最大的s-1条边,那D就是剩下n-s条边里最长的边的距离。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<math.h>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 const int N=1e3+5;
 8
 9 struct node2{
10     int x,y;
11 }a[N];
12
13 struct node{
14     int x,y;
15     double dis;
16     node(){}
17     node(int x,int y,double dis){
18         this->x=x;
19         this->y=y;
20         this->dis=dis;
21     }
22     bool operator <(const node &b)const{
23         return dis<b.dis;
24     }
25 }edge[N*N];
26 int root[N];
27
28 int find(int x){
29     return root[x]==x?x:root[x]=find(root[x]);
30 }
31
32 int main(){
33     int t;
34     scanf("%d",&t);
35     while(t--){
36         int s,n;
37         scanf("%d%d",&s,&n);
38         for(int i=1;i<=n;i++){
39             scanf("%d%d",&a[i].x,&a[i].y);
40             root[i]=i;
41         }
42         int cnt=0;
43         for(int i=1;i<=n;i++){
44             for(int j=i+1;j<=n;j++){
45                 double dis=sqrt(1.0*(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
46                 edge[++cnt]=node(i,j,dis);
47             }
48         }
49         sort(edge+1,edge+1+cnt);
50
51         int    edge_num=0;
52         double ans;
53         for(int i=1;i<=cnt;i++){
54             int x=find(edge[i].x);
55             int y=find(edge[i].y);
56             if(x!=y){
57                 edge_num++;
58                 root[x]=y;
59                 //从最小生成树中的n-1条边,去掉最大的s-1条边(因为有s个卫星站,相当于s个点,则s-1条边)
60                 //剩下的n-s条边中,最大的边长即为所求
61                 if(edge_num==n-s){
62                     ans=edge[i].dis;
63                     break;
64                 }
65             }
66         }
67         printf("%.2f\n",ans);
68     }
69     return 0;
70 }
时间: 2024-11-05 19:29:29

POJ 2349 Arctic Network(最下生成树+求第k大边)的相关文章

poj 2349 Arctic Network MST/二分答案

poj 2349 Arctic Network 题目传送 Sol: 方法一: 贪心的想,发现n个点只需要n-1条边即可,求MST即可,再把MST中最大的m-1条边去掉,第m大就是答案. code: #include<string> #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define IL inline #define RG register

POJ 2349 Arctic Network 最小生成树题解

本题也是使用Prime和Kruskal都可以的最小生成树的题解. 本题一点新意就是:需要除去最大的S-1个距离,因为可以使用卫星覆盖这些距离. 技巧:建图建有向图,速度快点,不用计算两边. 这里使用Prime,因为是稠密图. #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <al

poj 2349 Arctic Network (prim算法)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10525   Accepted: 3470 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

poj 2349 Arctic Network(最小生成树的第k大边证明)

题目链接: http://poj.org/problem?id=2349 题目大意: 有n个警戒部队,现在要把这n个警戒部队编入一个通信网络, 有两种方式链接警戒部队:1,用卫星信道可以链接无穷远的部队. 2,用信号收发器可以链接周围d米以内的部队. 现在有s个卫星信道,问d最小是多少时能连接全部的警戒部队? 解题思路: 我是用求最小生成树,记录路径长度,对路径长度排序后,第k长的边就是答案, 但是队友是用最小k度限制生成树,因为我的方法它证明不了,也推翻不了~~~~, 最后我下去仔细想了想反证

POJ 2349 Arctic Network (最小生成树第K大(小)边)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13108   Accepted: 4256 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 2349 Arctic Network

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18447   Accepted: 5829 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

POJ 2349 Arctic Network (最小生成树)

Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver a

poj 2349 Arctic Network(prime)

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25165   Accepted: 7751 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are t

hdu 2985 The k-th Largest Group 树状数组求第K大

The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted: 2712 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to g