首页 技术 正文
技术 2022年11月20日
0 收藏 415 点赞 3,112 浏览 2843 个字

题目连接

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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289