全程double精度就能过了 间接0距离不用管 prim自动连起来的

G++交的话只能用%f输出 C++的话加不加l都可以 (这么说以后用%f肯定不会错咯)

不过我不懂为什么他们的空间时间差了好多倍...

#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std; struct Node{double x, y, z, r;}node[]; typedef pair<double, int> pdi;
struct cmp{
bool operator () (const pdi a, const pdi b){
return a.first > b.first;
}
}; double val[][]; double nodeDist(Node a, Node b){
return max(0.0, sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)
+ (a.z-b.z)*(a.z-b.z)) - a.r - b.r);
} double prim(int n, int s)
{
double ans = , dist[];
bool vis[];
memset(vis, false, sizeof vis);
priority_queue<pdi, vector<pdi>, cmp> q; for(int i = ; i <= n; i++){
dist[i] = val[s][i];
q.push(make_pair(dist[i], i));
}
vis[s] = true;
while(!q.empty()){
pdi u = q.top();
q.pop();
if(vis[u.second]) continue;
vis[u.second] = true;
ans += u.first;
for(int i = ; i <= n; i++){
if(!vis[i] && dist[i] > val[u.second][i]){
dist[i] = val[u.second][i];
q.push(make_pair(dist[i], i));
}
}
}
return ans;
} int main()
{
int n;
while(scanf("%d", &n), n){
for(int i = ; i <= n; i++){
scanf("%lf%lf%lf%lf", &node[i].x, &node[i].y, &node[i].z, &node[i].r);
for(int j = ; j <= i; j++){
val[i][j] = val[j][i] = nodeDist(node[i], node[j]);
}
}
printf("%.3f\n", prim(n, ));
}
return ;
}

kuangbin_MST C (POJ 2031)的更多相关文章

  1. POJ 2031 prim

    Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2 ...

  2. Building a Space Station POJ - 2031

    Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...

  3. poj 2031 Building a Space Station(prime )

    这个题要交c++, 因为prime的返回值错了,改了一会 题目:http://poj.org/problem?id=2031 题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能 ...

  4. POJ - 2031 Building a Space Station(计算几何+最小生成树)

    http://poj.org/problem?id=2031 题意 给出三维坐标系下的n个球体,求把它们联通的最小代价. 分析 最小生成树加上一点计算几何.建图,若两球体原本有接触,则边权为0:否则边 ...

  5. (最小生成树) Building a Space Station -- POJ -- 2031

    链接: http://poj.org/problem?id=2031 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6011 ...

  6. POJ 2031 Building a Space Station【经典最小生成树】

    链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  7. POJ - 2031 Building a Space Station 【PRIME】

    题目链接 http://poj.org/problem?id=2031 题意 给出N个球形的 个体 如果 两个个体 相互接触 或者 包含 那么 这两个个体之间就能够互相通达 现在给出若干个这样的个体 ...

  8. Building a Space Station POJ 2031 【最小生成树 prim】

    http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...

  9. poj 2031 Building a Space Station【最小生成树prime】【模板题】

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepte ...

随机推荐

  1. 使用java自带线程池管理器demo

    1.程序入口:DabianTest package com.lbh.myThreadPool.present; import java.util.concurrent.ExecutorService; ...

  2. Bootstrap3.0学习教程十七:JavaScript插件模态框

    这篇文章中我们主要来学习一下JavaScipt插件模态框.在学习模态框之前,我们先来了解一下JavaScript插件吧. JavaScript插件概览 插件可以单个引入(使用Bootstrap提供的单 ...

  3. Asp.net图片文件上传

    对课本上的代码进行了一点的优化 1.获取文件的名称和文件的后缀名 引用了System.IO, 用Path.GetFileNamehe()取得文件名和Path.GetExtension获取文件的后缀 2 ...

  4. .NET WebForm 简介

    WebForm是微软开发的一款产品,它将用户的请求和响应都封装为控件.让开发者认为自己是在操作一个windows界面.极大地提高了开发效率. 在学习WebForm时,其知识量比WinForm要多,在实 ...

  5. MFC List Control 控件添加单元格编辑,实现可编辑重写

    在实现随机生成四则运算的个人项目中,目前已经完成基本功能,想要把程序变成一个Windows界面的程序.原本以为学习过MFC,应该很快就能完成.但是由于以前用的都是VC6.0,这次用了VS2010,稍微 ...

  6. 求助,JS 方法调用的问题,写了返回值,确调用时返回Undefined(新人第一次写)

    文本框txtUserName 的Onbulr 绑定方法 txtUserNameOnblur  这可以正常执行 而下面的btnRegister 的click 事件则无法正常调用    txtUserNa ...

  7. UE3植被工具-支持刷Actor)

    [目标] 植被工具-刷Actor [思路] 1 添加类型FFoliageMeshInfo.AddInstance 的函数 2 添加Instance就直接SpawnActor 3 类结构 4 修改的函数 ...

  8. IEnumerable接口的Aggregate方法

    以前小猪为了累加一个集合中的类容通常会写出类似这样的C#代码: string result ="": foreach (var item in items) { result+=i ...

  9. Java MVC Controller 中通过不同方式获取 @PathVariable 参数值

    1.最常用,也是最直接使用方法,通过@PathVariable注解获取 @RequestMapping(value = "/test/{a}") public @ResponseB ...

  10. css定位之浮动定位

    浮动定位可以是原本垂直排列的块级元素,变成水平排列 1浮动元素 float:left 或者float:right  这些浮动会直接碰到父容器的边界为止. 2设置了浮动的元素,元素会脱离标准文档流中,但 ...