题意思路:
https://www.cnblogs.com/yueshuqiao/archive/2011/08/24/2152471.html

Kruskal:

参考大佬的代码:
  • 法一
  • 法二
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 505;struct point
{double x, y;
};
point a[maxn];//读入点struct EDGE
{int u, v;double w;
};
bool cmp(EDGE a, EDGE b)
{return a.w < b.w;
}EDGE edge[maxn * maxn];//计算之后用于存储边int n;
int s, p;
int par[maxn];
double ans[maxn * maxn];
int cnt = 0;//数组下标计数int find(int x)
{if (par[x] == x)return par[x];return par[x] = find(par[x]);
}void initialize()
{cnt = 0;for (int i = 0; i < p; i++)par[i] = i;
}double cal_dis(double x1, double y1, double x2, double y2)
{return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}int main()
{cin >> n;while (n--){cin >> s >> p;initialize();for (int i = 0; i < p; i++)cin >> a[i].x >> a[i].y;for (int i = 0; i < p; i++)for (int j = 0; j < p; j++){edge[cnt].u = i;edge[cnt].v = j;edge[cnt].w = cal_dis(a[i].x, a[i].y, a[j].x, a[j].y);cnt++;}sort(edge, edge + cnt, cmp);int t = p;int cnt2 = 0;//int t = 0;for (int i = 0; i < cnt; i++){int fa = find(edge[i].u);int fb = find(edge[i].v);if (fa != fb){par[fb] = fa;ans[cnt2++] = edge[i].w;t--;if (t == 1)break;/*ans[t++] = edge[i].w;if (t == p - 1)break;*/}}//sort(ans, ans + t);printf("%.2lf\n", ans[p - s - 1]);/*for (int i = 0; i <= cnt2; i++)cout << ans[i] << " ";*//*for (int i = 0; i <= t; i++)cout << ans[i] << " ";*/}return 0;
}
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 505;struct point
{double x, y;
};
point a[maxn];//读入点struct EDGE
{int u, v;double w;
};
bool cmp(EDGE a, EDGE b)
{return a.w < b.w;
}EDGE edge[maxn * maxn];//计算之后用于存储边int n;
int s, p;
int par[maxn];
double ans[maxn * maxn];
int cnt = 0;//数组下标计数int find(int x)
{if (par[x] == x)return par[x];return par[x] = find(par[x]);
}void initialize()
{cnt = 0;for (int i = 0; i < p; i++)par[i] = i;
}double cal_dis(double x1, double y1, double x2, double y2)
{return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}int main()
{cin >> n;while (n--){cin >> s >> p;initialize();for (int i = 0; i < p; i++)cin >> a[i].x >> a[i].y;for (int i = 0; i < p; i++)for (int j = 0; j < p; j++){edge[cnt].u = i;edge[cnt].v = j;edge[cnt].w = cal_dis(a[i].x, a[i].y, a[j].x, a[j].y);cnt++;}sort(edge, edge + cnt, cmp);/*int t = p;int cnt2 = 0;*/int t = 0;for (int i = 0; i < cnt; i++){int fa = find(edge[i].u);int fb = find(edge[i].v);if (fa != fb){par[fb] = fa;/*ans[cnt2++] = edge[i].w;t--;if (t == 1)break;*/ans[t++] = edge[i].w;if (t == p - 1)break;}}sort(ans, ans + t);printf("%.2lf\n", ans[p - s - 1]);/*for (int i = 0; i <= t; i++)cout << ans[i] << " ";*/}return 0;
}

poj 2439 ArcticNetwork 最小生成树Kruskal、(Prim方法还没做相关推荐

  1. poj 1287 Networking 最小生成树 Kruskal Prim

    关于Kruskal和Prim在前面已经有详细的解释以及模板了 有关于需要注意的地方,以及在代码中注释出来. //Kruskal //用结构体保存起始点以及耗费,然后排序后,根据Kruskal #inc ...

  2. 【转载】突然觉得今年的时间过得太快了,不知不觉中还剩下四个多月就步入2017年了,回首一下,好像什么都还没做呢。...

    2019独角兽企业重金招聘Python工程师标准>>> 突然觉得今年的时间过得太快了,不知不觉中还剩下四个多月就步入2017年了,回首一下,好像什么都还没做呢. 在职场的时候,天天盼 ...

  3. Suzy找到实习了吗 | 字符串结束啦 今天学习kmp 题还没做!!!记得回来补!!!

    kmp 看完了kmp的讲解视频 还没做题,跳过了 先占坑 再来补!!!!

  4. poj 3026 BorgMaze 最小生成树Kruskal、Prim(Prim VS报错待解决

    题意 以及 思路: 从S点有一伙人出发去消灭A点的敌人,在S点或者A点可以分裂成几个小队然后分别走,这样路径=总队路径+各个小队路径 问你怎样路径最短. 在一个y行 x列的迷宫中,有可行走的通路空格' ...

  5. * poj 1251 JungleRoad 最小生成树 Kruskal算法、Prim算法

    文章目录 Kruskal算法 模板:https://blog.csdn.net/Rain722/article/details/65642992 Prim算法 模板: poj 1251 JungleR ...

  6. 我有一个顶会idea还没做实验,NeurIPS:先占坑再实验!

    作者 | 青 暮 相信大家对费马大定理都不陌生,x^n +y^n=z^n.一个简单无比的方程式却是难到几百年后才被数学家解决.而这其中最令人着迷的,当属费马当年在手稿留下的一句话:我这里有一个绝妙的证 ...

  7. 如果你还没冒犯过别人,说明你可能还没做过一件重要的事

    有意义的成就,往往会打扰到你周围的世界. 你在减肥吗?你应该为你自已的身体而开心!你在救助非洲的孩子?你应该救助你自己的国家!治愈了癌症?是什么让你坚持到最后? [你解决了世界贫困?很好.请问我现在该 ...

  8. 还没做2022年计划?这个超赞工具送给你

    又到了一年制定计划的时候了. 你可能要说,每年的计划在年终回顾时,都实现不了,除了发个朋友圈过下瘾,计划还有什么用呢? 这里其实是2个问题: 1.计划如何实现? 2.计划有什么用? 01 对于计划如何 ...

  9. poj 1751 Highways 最小生成树Kruskal(、Prim还没写

    OJ交不了,,, #pragma warning(disable:4996) #include<iostream> #include<string> #include<c ...

最新文章

  1. git branch 为什么会进入编辑状态_gitamp;github(总结git与github的基本用法)
  2. Map的4种遍历方法
  3. C++ POD(Plain Old Data)类型
  4. 第一章:渗透测试之信息搜集
  5. IO概述、异常、File文件类_DAY19
  6. java 无法注入service_SpringBoot集成shiro,MyRealm中无法@Autowired注入Service的问题
  7. xmind-HTTP协议
  8. HTTP协议编程,实现文件上传,Android客户端代码
  9. 中国电信:5G 手机可实现不换卡号;新西兰否认禁用华为;Visual Studio 2019 正式发布!| 极客头条...
  10. z=rand()%i c语言,C语言关于产生随机数文章转载两篇(一)
  11. VARCHART XGantt甘特图具有更多功能的HTML5 / Gantt图表的可视计划小部件
  12. tiny4412移植U-Boot 2020.07
  13. 只读存储器和随机存储器有什么区别?
  14. arcgis打开Excel文件显示没有注册类的解决方案
  15. vue中使用keep-alive无效以及include 和 exclude用法
  16. 操作系统实验Ucore:Kernel_init(四)
  17. ZZULIOJ1198: 考试排名(二)(结构体专题)
  18. 使用接口测试活动的中奖概率(随机事件测试)
  19. CDN和双线机房的区别
  20. 巧用ecshop做淘宝客

热门文章

  1. node之http模块总结
  2. 转:PriorityQueue
  3. FineReport:关于扩展行列求各种条件下的函数运用
  4. ECMAScript6 ES6语法
  5. jQuery Form 表单提交插件-----ajaxSubmit() 的应用
  6. leetcode 241 python
  7. 数据结构—链表-循环链表
  8. 数据3分钟丨MariaDB将借壳上市;前融云CTO杨攀加入涛思数据;​Elastic 8.0正式发布...
  9. 国考最热岗位报录比20602:1?还是数据库知识挑战赛适合我
  10. 【广州】openGauss Meetup (12月19日)| 活动预告