POJ-2349 Arctic Network---MST的第m长的边

题目链接:

https://vjudge.net/problem/POJ-2349

题目大意:

要在n个节点之间建立通信网络,其中m个节点可以用卫星直接连接,剩下的节点都要用线路连接,求剩下这些线路中最大的长度需要多长

思路:

还是MST的裸题,由于有m个节点可以用卫星连接,所以求出MST后,最长的m-1条边都可以用卫星连接,只需要求出第m长的边即可,直接用prim算法,保存mst每条边的长度,排序后直接输出第m长的边。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stack>
 8 #include<map>
 9 #include<set>
10 #include<sstream>
11 using namespace std;
12 typedef long long ll;
13 typedef pair<int, int> Pair;
14 const int maxn = 1e3 + 10;
15 const int INF = 1 << 30;
16 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
17 int T, n, m;
18 double Map[maxn][maxn];//存图
19 double lowcost[maxn], ans[maxn];
20 int mst[maxn], tot;
21 Pair a[maxn];
22 void prim(int u)//最小生成树起点
23 {
24     for(int i = 1; i <= n; i++)//初始化两个数组
25     {
26         lowcost[i] = Map[u][i];
27         mst[i] = u;
28     }
29     mst[u] = -1;//设置成-1表示已经加入mst
30     for(int i = 1; i <= n; i++)
31     {
32         double minn = INF;
33         int v = -1;
34         //在lowcost数组中寻找未加入mst的最小值
35         for(int j = 1; j <= n; j++)
36         {
37             if(mst[j] != -1 && lowcost[j] < minn)
38             {
39                 v = j;
40                 minn = lowcost[j];
41             }
42         }
43         if(v != -1)//v=-1表示未找到最小的边,
44         {//v表示当前距离mst最短的点
45             //printf("%d %d %d\n", mst[v], v, lowcost[v]);//输出路径
46             ans[tot++] = lowcost[v];
47             mst[v] = -1;
48             for(int j = 1; j <= n; j++)//更新最短边
49             {
50                 if(mst[j] != -1 && lowcost[j] > Map[v][j])
51                 {
52                     lowcost[j] = Map[v][j];
53                     mst[j] = v;
54                 }
55             }
56         }
57     }
58     //printf("weight of mst is %d\n", sum_mst);
59     sort(ans, ans + tot);
60     printf("%.2f\n", ans[n - m - 1]);//输出第m长的边
61 }
62 int main()
63 {
64     cin >> T;
65     while(T--)
66     {
67         cin >> m >> n;
68         for(int i = 1; i <= n; i++)cin >> a[i].first >> a[i].second;
69         for(int i = 1; i <= n; i++)
70         {
71             for(int j = 1; j <= n; j++)
72                 Map[i][j] = Map[j][i] = sqrt((a[i].first - a[j].first) * (a[i].first - a[j].first) + (a[i].second - a[j].second) * (a[i].second - a[j].second));
73         }
74         tot = 0;
75         prim(1);
76     }
77     return 0;
78 }

原文地址:https://www.cnblogs.com/fzl194/p/8727421.html

时间: 2024-08-30 15:27:48

POJ-2349 Arctic Network---MST的第m长的边的相关文章

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

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 (最小生成树第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(最小生成树的第k大边证明)

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

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

题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两个前哨之间的距离不超过D,才能通过无线电进行通信.求出能使所有前哨都能直接或间接通信的最小的D. 解题思路:题目要求使所有前哨都能直接或间接通信,那么相当于使n个点相连,至少需要n-1条边.可以将n个点分为s个团,每个团内部时无限通信,团与团之间通过卫星通信.那么就相当于用s个卫星装置建立s-1条边

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

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

2349 Arctic Network(中文版)

试题描述: 国防部希望通过无线网络连接几个北方前哨基地. 在建立网络时将使用两种不同的通信技术:每个前哨基站都将拥有无线电收发器,另外还有一些前哨卫星通道. 任何带卫星频道的两个前哨都可以通过卫星进行通信,无论其位置如何. 否则,只有两个前哨基站之间的距离不超过D,才能通过无线电通信,这取决于收发器的功  率.  更高的功率产生更高的D,但成本更高. 由于采购和维护的考虑,前哨收发器必须相同; 也就是说,D的价值对于每对前哨都是一样的. 您的工作是确定收发器所需的最小D. 在每对前哨之间必须至少