题目连接

http://poj.org/problem?id=2560

Freckles

Description

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through. 
Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

Input

The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

Output

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

Sample Input

3
1.0 1.0
2.0 2.0
2.0 4.0

Sample Output

3.41
$n$个点用$Prim$求最小生成树,开始用的$double$类型$\%lf$控制精度$g++$不停地wa后改为$float,\%f$过了/(ㄒoㄒ)/~~

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
using std::set;
using std::pair;
using std::swap;
using std::multiset;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 110;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
struct P {float x, y;P(float i = 0.0, float j = 0.0) :x(i), y(j) {}inline float calc(const P &k) const {return sqrt((x - k.x) * (x - k.x) + (y - k.y) * (y - k.y));}
}A[N];
struct PDI {int v;float s;PDI(int i = 0, float j = 0.0) :v(i), s(j) {}inline bool operator<(const PDI &k) const {return s > k.s;}
};
struct Prim {bool vis[N];int tot, head[N];float mincost[N];struct edge { int to; float w; int next; }G[(N * N) << 1];inline void init(int n) {tot = 0;rep(i, n + 1) {head[i] = -1;vis[i] = false;mincost[i] = INF;}}inline void add_edge(int u, int v, float w) {G[tot] = (edge){ v, w, head[u] }; head[u] = tot++;}inline void built(int n) {rep(i, n) scanf("%f %f", &A[i].x, &A[i].y);for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {if (i == j) continue;add_edge(i + 1, j + 1, A[i].calc(A[j]));}}}inline void prim(int s = 1) {float ans = 0.0;priority_queue<PDI> q;q.push(PDI(s));for (int i = head[s]; ~i; i = G[i].next) {edge &e = G[i];q.push(PDI(e.to, mincost[e.to] = e.w));}vis[s] = true;while (!q.empty()) {PDI t = q.top(); q.pop();int u = t.v;if (vis[u]) continue;vis[u] = true;ans += mincost[u];for (int i = head[u]; ~i; i = G[i].next) {edge &e = G[i];if (mincost[e.to] > e.w && !vis[e.to]) {q.push(PDI(e.to, mincost[e.to] = e.w));}}}printf("%.2f\n", ans);}inline void solve(int n) {init(n), built(n), prim();}
}go;
int main() {
#ifdef LOCALfreopen("in.txt", "r", stdin);freopen("out.txt", "w+", stdout);
#endifint n;while (~scanf("%d", &n)) {go.solve(n);}return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4780795.html

poj 2560 Freckles相关推荐

  1. #最小生成树,prim,kruskal#poj 2560 Freckles 雀斑

    题目 求最小生成树 分析 prim & kruskal Kruskal代码 #include <cstdio> #include <cmath> #include &l ...

  2. POJ 2560 雀斑

    目录 题目链接 题目大意 思路 代码 题目链接 http://poj.org/problem?id=2560 题目大意 有 n<=100 n <= 100 n个点,给定各个点的坐标,求这张 ...

  3. 【Q】【POJ 2560】【POJ 2031】

    水题两道,比较裸的prim~ 注意double类型的数组用memset无穷大不老好用的,还是for好用且保险,嗯嗯~ 转载于:https://www.cnblogs.com/sawoman/archi ...

  4. 最小生成树之Kruskal

    模板题,学习一下最小生成树的Kruskal算法 对于一个连通网(连通带权图,假定每条边上的权均为大于零的实数)来说,每棵树的权(即树中所有边的权值总和)也可能不同 具有权最小的生成树称为最小生成树 生 ...

  5. 2019.9.19最小生成树知识点总结

    ​​​​​HDU 1102 Constructing Roads(最小生成树-Prim) 最常见的,将已建成的路的权值设置为0,求最小生成树! HDU 1162 Eddy's picture(最小生成 ...

  6. 滤波的概念和作用(滤波器、掩模、核、模板、窗口是一个意思)

    转载自:https://blog.csdn.net/mvtechnology/article/details/45041771 图像滤波增强处理实质上就是运用滤波技术来增强图像的某些空间频率特征,以改 ...

  7. POJ 图论---1_Uriel's Corner Uriel's Coding Learning Cubing Zone

    原文地址: http://www.cppblog.com/Uriel/articles/121814.html 内容: 刚开始学图论不久,这个是自己做过的一点图论水题,不一定全,有什么错误或者大家有其 ...

  8. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  9. POJ2560-雀斑(Freckles)【图论,并查集,最小生成树,KURUSKAL】

    正题 题目链接: http://poj.org/problem?id=2560 大意 有n个点,给出坐标,求连接这n个点的最短路线 解题思路 KURUSKAL算法求最小生成树. 代码 #include ...

  10. POJ 3268 D-Silver Cow Party

    http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

最新文章

  1. Java封装(速读版)
  2. 【Ubuntu】ubuntu webqq桌面版pywebqq
  3. Replication主要配置项
  4. 阿里云虚拟机mysql_打开虚拟机里mysql
  5. JUnit+EclEmma进行覆盖测试
  6. android中屏幕宽高显示不全,Android 获取屏幕宽度跟高度
  7. C语言笔试常考知识点
  8. c++代码健壮性_复活Navex-使用图查询进行代码分析(上)
  9. caspase3是什么意思_caspase-3
  10. python利器-python利器app下载-python利器手机版 _5577安卓网
  11. 设置背景色为渐变色 css
  12. 【游戏】基于matlab GUI时钟设计【含Matlab源码 1102期】
  13. Ubuntu 定时开关机
  14. MySQL获取汉字拼音首字母
  15. 树莓派-硬件基础GDIO管脚(5)
  16. 自动化学科前沿讲座作业 基于深度学习的工厂人员监测系统设计
  17. 如何在体育场创造极致观看体验
  18. 金融应用:信用卡号的合法性验证
  19. 嫦娥四号成功着陆月球背面,实现人类探测器首次在月球背面软着陆!
  20. 微信小程序之获取用户基本信息

热门文章

  1. hadoop jar 找不到main class_10年老架构,教你HadoopJob使用第三方依赖jar文件,不来就后悔吧...
  2. python使用正则验证电子邮件_在Python中使用正则表达式提取电子邮件地址
  3. java中对事件的监听事件,详谈Java中的事件监听机制
  4. C/C++[codeup 1923]排序
  5. 翻译:Google研究:回顾2020年并展望2021年 - Jeff Dean
  6. Swift TouchId指纹解锁,FaceId面部解锁
  7. 反地理编码 高德地图_由中文地址返回点位坐标-地理编码脚本分享
  8. this指针常识性问题
  9. Oracle 10g 数据库连接出现The Network Adapter could not establish the connection解决办法
  10. heeds matlab,Isight FD4-CAE优化软件 与HEEDS对比