题目连接

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 LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
while (~scanf("%d", &n)) {
go.solve(n);
}
return 0;
}

poj 2560 Freckles的更多相关文章

  1. POJ 2560 Freckles Prime问题解决算法

    这个问题正在寻求最小生成树. 给定节点的坐标,那么我们需要根据各个点之间的这些坐标来计算距离. 除了这是标准的Prime算法的,能源利用Prime基本上,你可以使用Kruskal. 经典的算法必须填写 ...

  2. Poj(2560),最小生成树,Prim

    题目链接:http://poj.org/problem?id=2560 只想说“全都是套路”,关键建图. #include <stdio.h> #include <string.h& ...

  3. POJ 2560

    #include<iostream> #include<algorithm> #include<cmath> #include<iomanip> #de ...

  4. 最小生成树之Kruskal

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

  5. 8月清北学堂培训 Day5

    今天是杨思祺老师的讲授~ 最短路练习题: POJ 1125 Stockbroker Grapevine 有 N 个股票经济人可以互相传递消息,他们之间存在一些单向的通信路径.现在有一个消息要由某个人开 ...

  6. DP&图论 DAY 5 上午

    DP&图论  DAY 5  上午 POJ 1125 Stockbroker Grapevine 有 N 个股票经济人可以互相传递消息,他们之间存在一些单向的通信路径.现在有一个消息要由某个人开 ...

  7. 又是图论.jpg

    BZOJ 2200 道路和航线重讲ww: FJ 正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到 T 个城镇 (1 ≤ T ≤ 25000),编号为 1 到 T.这些城镇之间通过 R 条 ...

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

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

  9. POJ 1135.Domino Effect Dijkastra算法

    Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10325   Accepted: 2560 De ...

随机推荐

  1. Activity使用Dialog样式导致点击空白处自动关闭的问题

    将Activity设置成窗口的样式实现Dialog或者Popupwindow效果在开发中是很常用的一种方式,在AndroidMenifest.xml中将需要设置的Activity增加android:t ...

  2. 转)SSO单点登录在互联网电商应用中的解决方案(基于CAS的改造)

    电商平台中无论是前端还是后端会存在大量的业务应用,在整个交易的过程中请求是在各个业务应用中流转的,对于用户来讲只需要登录一次就可以访问所有的业务,这就是单点登录SSO. 单点登录开源有很多的解决方案, ...

  3. [drp 5] pageModel的建立,实现分页查询

    导读:之前做的分页,一直都是用的easy--UI分页,然后没有系统的整理过,就是知道传几个参数,然后云云.这次,从头到尾总结一下,了了我的这桩心愿.人事系统的重定向工作,一直刺激着我一定要总结总结这个 ...

  4. rsync 实现实时增量备份

    Rsync + Crontab实现定时文件同步(首次全量+后续增量) 2015-04-14 19:02:11 标签:增量更新 rsync crontab 原创作品,允许转载,转载时请务必以超链接形式标 ...

  5. dedecms后台登录如何去除验证码设置

    dedecms后台验证有时间输入总是不对,有时候却不显示,而输入验证码无疑是一个麻烦的过程,那么我们怎么样来去除后台验证码,实现输入帐号密码直接登录呢?我来为大家介绍一下: 让人感到烦恼的情况出现了! ...

  6. jquery.css 最简单的用法

      //判断验证码     if (pwd != "" && pwd != null) {         $("#TxtPwd").css(& ...

  7. (笔记)angular 事件传递获取当前

  8. phalcon框架学习之router

    router定义 在DI中注册router的方法: $di->set('router', function(){ $router = new Phalcon\Mvc\Router(); $rou ...

  9. 2. XAML

    1. 什么是 XAML XAML 可以说是 XML 的一个特殊子集,使用相同的语法,只是 XML 可以自定义任何的节点和属性,但 XAML 是有所限制的,只能在规定的命名空间下使用. 2. names ...

  10. linux系统下将php和mysql命令加入到环境变量中的方法

    在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行 “php -v”命令查看当前php版本信息时时 ...