最小生成树(prim和Kruskal操!!SB题)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 30571 | Accepted: 9220 | 
Description
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
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
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
Source
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <map>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1024
using namespace std;
typedef long long ll ;
int s , n ;
double ma[][];
double dis[] ;
double d[];
int vis[] ;
int ans ;
struct node{
double x , y ;
}a[]; void prim()
{
for(int i = ; i <= n ; i++)
{
dis[i] = ma[][i];
}
vis[] = ;
for(int i = ; i < n ; i++)
{
double min = INF ;
int pos ;
for(int j = ; j <= n ; j++)
{
if(!vis[j] && dis[j] < min)
{
min = dis[j];
pos = j ;
}
}
vis[pos] = ;
d[ans++] = min ;
for(int j = ; j <= n ; j++)
{
if(!vis[j] && dis[j] > ma[pos][j])
{
dis[j] = ma[pos][j];
}
}
}
} bool cmp(double a , double b)
{
return a > b ;
} void init()
{
memset(vis , , sizeof(vis));
for(int i = ; i <= n ; i++)
{
for(int j = ; j <= n ; j++)
{
ma[i][j] = INF;
}
}
ans = ;
} int main()
{
int t ;
scanf("%d" , &t);
while(t--)
{
scanf("%d%d" , &s , &n);
init();
for(int i = ; i <= n ; i++)
{
scanf("%lf%lf" , &a[i].x , &a[i].y);
}
for(int i = ; i <= n ; i++)
{
for(int j = i + ; j <= n ; j++)
{
double w ;
w = sqrt((a[i].y - a[j].y) * (a[i].y - a[j].y) + (a[i].x - a[j].x)*(a[i].x - a[j].x)) ;
ma[i][j] = ma[j][i] = min(ma[i][j] , w);//这里if判断大小时,注意方向不要搞错了
}
}
prim();
sort(d , d + n - , cmp);
printf("%.2f\n" , d[s-]);
} return ;
}
Kruskal
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
int fa[] , ans , n , m , way ;
double ds[] ; struct Node
{
int from , to ;
int x ;
int y ;
double d;
}a[]; bool cmp(const Node &a , const Node &b)
{
return a.d < b.d ;
} int gcd(int x)
{
if(x == fa[x])
return x ;
else
return gcd(fa[x]);
} void unite(int x , int y)
{
x = gcd(x) ; y = gcd(y) ;
if(x > y) fa[x] = y ;
else fa[y] = x ;
} void init()
{
for(int i = ; i <= m ; i++)
fa[i] = i ;
ans = ;
way = ;
memset(ds , , sizeof(ds));
}
bool cmp1(double a , double b)
{
return a > b ;
} int main()
{
int t ;
cin >> t ;
while(t--)
{
cin >> n >> m ;
init();
for(int i = ; i <= m ; i++)
{
cin >> a[i].x >> a[i].y ;
}
for(int i = ; i <= m ; i++)
{
for(int j = i + ; j <= m ; j++)
{
a[way].from = i ;
a[way].to = j ;
a[way].d = sqrt(pow((double)(a[i].x - a[j].x) , ) + pow((double)(a[i].y - a[j].y) , ));
way++; }
}
sort(a , a + way , cmp) ;
for(int i = ; i < way ; i++)
{
if( ans == m - )
break ;
if(gcd(fa[a[i].from]) != gcd(fa[a[i].to]))
{
unite(a[i].from , a[i].to);
ds[ans] = a[i].d;
ans ++ ;
}
}
sort(ds , ds + ans , cmp1);
printf("%.2f\n" , ds[n-]); } return ;
}
最小生成树(prim和Kruskal操!!SB题)的更多相关文章
- poj1861 最小生成树 prim & kruskal
		
// poj1861 最小生成树 prim & kruskal // // 一个水题,为的仅仅是回味一下模板.日后好有个照顾不是 #include <cstdio> #includ ...
 - 图的最小生成树(Prim、Kruskal)
		
理论: Prim: 基本思想:假设G=(V,E)是连通的,TE是G上最小生成树中边的集合.算法从U={u0}(u0∈V).TE={}开始.重复执行下列操作: 在所有u∈U,v∈V-U的边(u,v)∈E ...
 - 最小生成树 Prim算法 Kruskal算法实现
		
最小生成树定义 最小生成树是一副连通加权无向图中一棵权值最小的生成树. 在一给定的无向图 G = (V, E) 中,(u, v) 代表连接顶点 u 与顶点 v 的边(即,而 w(u, v) 代表此边的 ...
 - 最小生成树Prim算法 Kruskal算法
		
Prim算法(贪心策略)N^2 选定图中任意定点v0,从v0开始生成最小生成树 树中节点Va,树外节点Vb 最开始选一个点为Va,其余Vb, 之后不断加Vb到Va最短距离的点 1.初始化d[v0]=0 ...
 - 最小生成树--Prim及Kruskal
		
//prim算法#include<cstdio> #include<cmath> #include<cstring> #include<iostream> ...
 - 最小生成树prim和kruskal模板
		
prim: int cost[MAX_V][MAX_V]; //cost[u][v]表示边e=(u,v)的权值(不存在的情况下设为INF) int mincost[MAX_V]; //从集合X出发的每 ...
 - 最小生成树Prim算法Kruskal算法
		
Prim算法采用与Dijkstra.Bellamn-Ford算法一样的“蓝白点”思想:白点代表已经进入最小生成树的点,蓝点代表未进入最小生成树的点. 算法分析 & 思想讲解: Prim算法每次 ...
 - 最小生成树 Prim和Kruskal
		
感觉挺简单的,Prim和Dijkstra差不多,Kruskal搞个并查集就行了,直接上代码吧,核心思路都是找最小的边. Prim int n,m; int g[N][N]; int u,v; int ...
 - Prim和Kruskal最小生成树
		
标题: Prim和Kruskal最小生成树时 限: 2000 ms内存限制: 15000 K总时限: 3000 ms描述: 给出一个矩阵,要求以矩阵方式单步输出生成过程.要求先输出Prim生成过程,再 ...
 
随机推荐
- V8引擎回收机制、 内存泄露
			
一.垃圾回收:将内存不在使用的数据进行清理,释放内存空间 v8将内存分为新生代空间和老生代的空间 新生代空间:用于存活较短的对象 :又分为二个空间:from空间和to空间 :Scav ...
 - lLinux的常用命令
			
命令基本格式: 命令提示符:[root@localhost ~]# root 代表当前的登录用户(linux当中管理员账号是root) @ 无实际意义 localhost ...
 - Docker实战部署应用——MySQL5.7
			
MySQL 部署 拉取MySQL镜像 拉取命令: docker pull mysql:5.7 查看镜像 docker images 创建 MySQL 容器 docker run -id --name= ...
 - 脚本_获取本机 MAC 地址
			
#!bin/bash#作者:liusingbon#功能:获取本机 MAC 地址ip a s |awk 'BEGIN{print "本机 MAC 地址信息如下:"}/^[0-9]/{ ...
 - 如何启用Nginx的status功能,查看服务器状态信息?
			
如何查看服务器状态信息? 我们可以通过安装Nginx的功能模块,并修改Nginx的主配置文件来实现. 1.编译安装时使用--with-http_stub_status_module开启状态页面模块 [ ...
 - ShellExecute指定IE浏览器打开
			
ShellExecute(NULL,L"open", L"iexplore.exe", L"www.baidu.com", NULL, SW ...
 - java并发学习--第二章 spring boot实现线程的创建
			
除了之前介绍的创建线程方式外,spring boot为我们了提供一套完整的线程创建方式,其中包括了:线程.线程池.线程的监控. 一.使用spring boot提供的方法创建线程与线程池 1.首先在sp ...
 - Nginx-常用命令和配置文件
			
Nginx常用命令 1.启动命令 在/usr/local/nginx/sbin 目录下执行 ./nginx 2.关闭命令 在/usr/local/nginx/sbin 目录下执行 ./nginx s ...
 - 031:verbatim 标签
			
verbatim 标签: verbatim 标签:默认在 DTL 模板中是会去解析那些特殊字符的.比如 {% 和 %} 以及 {{ 等.如果你在某个代码片段中不想使用 DTL 的解析引擎.那么你可以把 ...
 - Centos抓包方法
			
1. 安装tcpdump工具 rpm -ql tcpdump #查看tcpdump是否安装 本机是安装的,yum安装: yum install tcpdump 2. tcpdump抓包 根据协议和端 ...