POJ2349Arctic Network之最小生成树kruskal解法
来源:互联网 发布:j2ee服务器端高级编程 编辑:程序博客网 时间:2024/05/17 01:16
Arctic Network
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23142 Accepted: 7110
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 and some outposts will in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.
Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.
Input
The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).
Output
For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.
Sample Input
1
2 4
0 100
0 300
0 600
150 750
Sample Output
212.13
- 题意:有s个卫星信道和p个前哨站,有卫星信道的那s个前哨站可以直接通信,求最小生成树中最大的那条边的值.
- 分析:易知s个卫星信道的存在可以减少最小生成树中的s-1条边,无论这s-1条边是否相邻,那么生成最小生成树过程中,把每条边的值用数组保存起来,排序后ans[(V-1)-(s-1)-1]即为所求
#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <vector>#include <bitset>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;int n;int s,p;int V,E;double x[505];double y[505];double ans[505];int par[505];int ran[505];struct edge{ double v,to,cost;};struct edge ed[300000];void init(int n){ for (int i = 0; i < n; i += 1){ par[i] = i; ran[i] = 0; }}int find(int x){ if(par[x] == x) return x; else return par[x] = find(par[x]);}void unite(int x,int y){ x = find(x); y = find(y); if(x == y) return; if(x < y) par[x] = y; else{ par[y] = x; ran[x]++; }}bool same(int x,int y){ return find(x) == find(y);}int cmp(edge e1,edge e2){ return e1.cost < e2.cost;}void kruskal(){ V = p; sort(ed,ed+E,cmp); init(V); int k = 0; for (int i = 0; i < E; i += 1){ edge e = ed[i]; if (!same(e.v,e.to)){ unite(e.v,e.to); //数组赋值 ans[k] = e.cost; k++; } } sort(ans,ans+k); printf("%.2lf\n",ans[(V-1)-(s-1)-1]);}int main(){ cin >> n; while (n--){ cin >> s >> p; for (int i = 0; i < p; i += 1){ cin >> x[i] >> y[i]; } E = 0; for (int i = 0; i < p; i += 1){ for (int j = 0; j < p; j += 1){ ed[E].v = i; ed[E].to = j; ed[E].cost = sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2)); E++; } } kruskal(); } return 0;}
用kruskal解题纯粹是为了练习这个算法,但是这个方法显然写起来有点麻烦…
- POJ2349Arctic Network之最小生成树kruskal解法
- poj2349Arctic Network最小生成树
- POJ2349Arctic Network(最小生成树)
- poj2349Arctic Network (最小生成树运用)
- poj2349Arctic Network(最小生成树kruscal+第k长的边)
- poj 1861 Network (kruskal 最小生成树)
- poj2349 Arctic Network 最小生成树,kruskal
- poj1861 Network(kruskal求最小生成树)
- POJ2349 Arctic Network(最小生成树,Kruskal)
- 最小生成树之kruskal
- 最小生成树之Kruskal
- 最小生成树之 Kruskal
- 最小生成树之KRUSKAL
- 最小生成树之kruskal
- 最小生成树之kruskal
- 最小生成树之kruskal
- 最小生成树之Kruskal
- network 最小生成树 Kruskal 算法 poj 1861 zoj 1542
- 你不知道的JavaScript 上卷 第一部分 作用域和闭包
- 前台jQuery实现图片轮播
- Centos新增一个管理用户
- C语言小碎片五
- 网络编程总结--小码哥java
- POJ2349Arctic Network之最小生成树kruskal解法
- Java的HashMap
- 算法-快速排序
- 2017/10/03 学习笔记
- C语言只能在开头定义变量?
- [简单题]Counting Duplicates( Python 实现)
- 99%的人不满意现在的工作,可是然后呢?
- 请停止无效努力,让你的付出都能有所收获
- 【LeetCode】162.Find Peak Element 寻找峰值(二分法)