题目描述

Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.

Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.

给定 nnn 个点的坐标,第 iii 个点的坐标为 (xi,yi),这 n 个点编号为 1 到 n。给定 m 条边,第 i 条边连接第 ui​ 个点和第 vi 个点。现在要求你添加一些边,并且能使得任意一点都可以连通其他所有点。求添加的边的总长度的最小值。

输入格式

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Two space-separated integers: Xi and Yi

* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.

第一行两个整数 n,m 代表点数与边数。
接下来 n 行每行两个整数 xi,yi 代表第 i 个点的坐标。
接下来 m 行每行两个整数 ui,vi​ 代表第 iii 条边连接第 ui​ 个点和第 vi​ 个点。

输出格式

* Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.

一行一个实数代表添加的边的最小长度,要求保留两位小数,为了避免误差, 请用 64 位实型变量进行计算。

输入输出样例

输入 #1

4 1
1 1
3 1
2 3
4 3
1 4

输出 #1

4.00

说明/提示

数据规模与约定

说明

Translated by 一只书虫仔。

思路:

将已连的边 的边权设置成 0

再将未连接的所有点 之间的边连接起来(需要计算边权)

最后跑最小生成树,把其边权相加 就是答案!

ps:这个题卡map,判断是否已连接的时候换成bool 数组来判断,用map会T两个测试点

AC代码如下:

#include <bits/stdc++.h>
#define buff                     \ios::sync_with_stdio(false); \cin.tie(0);                  \cout.tie(0)
using namespace std;
const int N = 1e3 + 9;
int n, m;
struct point
{double x, y;
} s[N];
double g[N][N];
bool st[N];
double dist[N];
//map<pair<int,int>,int> mp;//map的查找速度是log(n)级别
//.......卡map
bool mp[N][N];
void add(int a, int b, bool flag)
{if (flag){g[a][b] = 0;g[b][a] = 0;return ;}double d = sqrt((s[a].x - s[b].x) * (s[a].x - s[b].x) + (s[a].y - s[b].y) * (s[a].y - s[b].y));g[a][b] = d;g[b][a] = d;
}
void init()
{for(int i = 1;i<=n;i++){dist[i] = 0x3f3f3f3f;}
}
double prim()
{init();double res = 0;for (int i = 0; i < n; i++){int t = -1;for (int j = 1; j <= n; j++){if (!st[j] && (t == -1 || dist[t] > dist[j]))t = j;}if (i && dist[t] == 0x3f3f3f3f)return 0x3f3f3f3f;if (i)res += dist[t];st[t] = 1;for (int j = 1; j <= n; j++){dist[j] = min(dist[j], g[t][j]);}}return res;
}
int main()
{buff;cin >> n >> m;for (int i = 1; i <= n; i++){cin >> s[i].x >> s[i].y;}for (int i = 1; i <= m; i++){int a, b;cin >> a >> b;add(a, b, 1);// mp[{a,b}] = 1;// mp[{b,a}] = 1;mp[a][b] = 1;mp[b][a] = 1;}for(int i = 1;i<=n;i++)for(int j = 1;j<=n;j++){if(!mp[i][j]&&i!=j)add(i,j,0);}double ans = prim();printf("%.2lf\n", ans);
}

P2872 [USACO07DEC]Building Roads S(最小生成树)相关推荐

  1. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

  2. USACO07DEC道路建设Building Roads(prim算法+堆优化与Kruskal+路径压缩对比)

    目录 primprimprim算法 KruskalKruskalKruskal算法 P2872 [USACO07DEC]道路建设Building Roads 4 1 1 1 3 1 2 3 4 3 1 ...

  3. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路(最小生成树)

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1709  Solved ...

  4. [BZOJ1626][Usaco2007 Dec]Building Roads 修建道路

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1730  Solved ...

  5. HDU 1301 Jungle Roads(裸最小生成树)

    题目链接 今天做了好几个模版最小生成树...贴一个kurskral. 1 /* 2 HDU 1301 Jungle Roads 3 最小生成树Kurskal模版 4 */ 5 #include < ...

  6. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads 题目链接HDU 题目链接POJ 题意: 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得随意牛棚两个 ...

  7. 【HDU - 1102】Constructing Roads (最小生成树裸题模板)

    题干: There are N villages, which are numbered from 1 to N, and you should build some roads such that ...

  8. Building Roads(POJ-3625)

    Problem Description Farmer John had just acquired several new farms! He wants to connect the farms w ...

  9. HDOJ1102 Constructing Roads【最小生成树】-----武科大ACM暑期集训队选拔赛1题

    这道题目没有做出来,代码写好之后一直没有AC,本以为做了这么多最小生成树的题目,这道题一定没问题的,结果很遗憾,没有注意细节问题: 首先,如何处理已经建好的路?已经建好的路说明这两个点是连通的,只要把 ...

最新文章

  1. java regex
  2. JNI的native代码中打印日志到eclipse的logcat中
  3. zxing 源码笔记
  4. 【转】为什么要写技术博
  5. 如何清理不必要的事件日志分类
  6. iOS开发UI篇—核心动画(基础动画)
  7. 三维旋转矩阵_第三讲:三维空间的刚体运动
  8. 二维数组求最小值_求一列中满足条件的最大最小值
  9. 第1章 神经网络的思想
  10. 三层交换机和链路聚合
  11. 图文配置docker阿里云加速器教程
  12. 应用标题及描述不可滥用关键字
  13. 宿华辞任快手CEO、程一笑接任;百世将国内快递业务68亿元出售给极兔|美通社头条...
  14. SLAM中边缘化与一致性
  15. docker仓库的搭建居然只要一分钟!
  16. Android-NuPlayer音视频同步之安卓Q新功能
  17. 谈谈市场上常用语音芯片方案选型,录音芯片方案选型
  18. [Constraints 18-5210] No constraints selected for write.
  19. PDM与Excel利用VB脚本进行互导
  20. 2021年低压电工免费试题及低压电工考试技巧

热门文章

  1. 使用python3.7配置开发钉钉群自定义机器人(2020年新版攻略)
  2. springboot ServletContextListener接口
  3. dockerfile写法和docker-compose和docker-compose.yml
  4. 测试开发面试技巧_面试技巧将给您带来信心并帮助您获得开发工作
  5. ubuntu pycharm设置快捷图标_这些Ubuntu中的小技巧,你知道吗?
  6. vue 设置输入法隐藏_iPhone键盘的12种隐藏用法,超好用!可惜没几个人知道!
  7. docker中使用postgresql
  8. Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_
  9. 数据库引索的简单了解
  10. 数据洞察 | Python解读地摊——你想好摆摊去卖什么了吗?