最小生成树(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生成过程,再 ...
随机推荐
- MongoDB与关系数据库的对比
MongoDB与关系数据库的对比
- 07.SUSE Linux 系统本地yum源配置
SUSE Linux 系统 1.新建本地源存储目录root@suse:mkdir /mnt/SUSE_LOCAL_SOURCE 2.创建zypper本地源root@suse:zypper ar fil ...
- JFreeChart使用
最近项目需要做图形分析,就想到了使用JFreeChart,之前也没有使用过,就现学先用吧.本文主要记录一些主要的代码及学习使用过程. 使用JFreeChart步骤: 一.下载JFreeChart.ja ...
- AGC036C GP 2
由于近期集训做的一直都是校内题 然后好久都怎么写题了( 发篇博客证明我还活着 (其实也没人关心 好像并不是很难的一道计数 就是脑子总是缺一块导致会做不出来( 首先我们可以分析性质 1.$\sum A_ ...
- 图片公式转为word格式
mathpix提取Latex格式,下载mathpix snipping tool工具,截图即可获取Latex格式公式 Latex格式去下面网站转换为mathml格式 https://johnmacfa ...
- Python---基础-小游戏用户猜数字2
一.使用int()将小数转换成整数,结果是向上取数还是向下取数 int(3,4) print(int(3,4)) ####写一个程序,判断给定年份是否为闰年 - 闰年的定义,能够被4整除的年份就叫闰年 ...
- Json转换 在java中的应用
Json转换辅助类比较多,比如谷歌的Gson,阿里的FastJson,Jackson.net.sf.json等等 用了一圈后,本人还是比较推荐用net.sf.json 这里就介绍一下net.sf.js ...
- Android逆向之旅---静态分析技术来破解Apk
一.前言 从这篇文章开始我们开始我们的破解之路,之前的几篇文章中我们是如何讲解怎么加固我们的Apk,防止被别人破解,那么现在我们要开始破解我们的Apk,针对于之前的加密方式采用相对应的破解技术,And ...
- 给数据库用户授权(对象多为系统表,如dba可以查看的表)
我们知道,创建一个新用户时,网上各种的帖子包括书籍中经常用到一个grant connect,resource to user;,这样才能用这个用户登录数据库,那么这条语句的真正作用是什么呢? 首先,g ...
- macOS 10.15 Catalina Apache设置:多个PHP版本
第1部分:macOS 10.15 Catalina Web开发环境 在macOS上开发Web应用程序真是令人高兴.有许多设置开发环境的选项,包括广受欢迎的MAMP Pro,它在Apache,PHP和M ...