最小生成树(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生成过程,再 ...
随机推荐
- Java JNA (五)—— 释放Memory对象分配的内存
Java进程的内存包括Java NonHeap空间.Java Heap空间和Native Heap空间. JNA中的Memory对象是从Native Heap中分配空间.但java的GC是针对Java ...
- C++基础之static(静态)变量
static 表示静态 作用: 1.在函数体内,静态变量的值维持不变(记忆功能) 2.是一个本地的全局函数,即只能被本模块的函数访问(隐藏功能) static变量: static全局变量和普通 ...
- Linux系统Docker启动问题Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service"
在Liunx中使用Docker, 注: Liunx使用的是在虚拟机下的centOS7版本在刚开始安装Docker时没有任何错误, 但是在后续的docker启动过程中, 出现以下问题: [root@zk ...
- bzoj5178 [Jsoi2011]棒棒糖 主席树+线段树二分
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5178 https://lydsy.com/JudgeOnline/problem.php?id ...
- Vue的自定义滚动,我用el-scrollbar
弄了一个持续更新的github笔记,可以去看看,诚意之作(本来就是写给自己看的--)链接地址:Front-End-Basics 此篇文章的地址:Vue的自定义滚动,我用el-scrollbar 基础笔 ...
- HTML表单(来自MDN的总结)
表单介绍 HTML表单是用户和web站点或应用程序之间交互的主要内容之一.它们允许用户将数据发送到web站点.大多数情况下,数据被发送到web服务器,但是web页面也可以拦截它自己并使用它. HTML ...
- 6.dockerfile
一.概述 自制镜像的目的不是为了解决配置更新的问题,而是为了定制化应用服务. 镜像的制作:基于容器制作:dockerfile dockerfile的格式:注释信息+指令(约定俗成使用大写)及其参数 d ...
- Centos7.5中的SElinux操作命令说明
设置Selinux模式 setenforce 0 0表示警告模式 1表示强制模式 关闭要设置/etc/sysconfig/selinux下将"SELINUX=enforcing"改 ...
- R语言 Keras Training Flags
在需要经常进行调参的情况下,可以使用 Training Flags 来快速变换参数,比起直接修改模型参数来得快而且不易出错. https://tensorflow.rstudio.com/tools/ ...
- linux下将一系列.o文件打包成.a文件
参考链接:https://www.cnblogs.com/joshtao/p/7380627.html