C - Building a Space Station - poj 2031
2015-07-24 19:58
423 查看
空间站是有一些球状的房间组成的,现在有一些房间但是没有相互连接,你需要设计一些走廊使他们都相通,当然,有些房间可能会有重合(很神奇的样子,重合距离是0),你需要设计出来最短的走廊使所有的点都连接。分析:因为给的都是点的坐标,所以构图的时候会有一些麻烦,不过也仅此而已。。。*******************************************************************
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
#define maxn 105
struct point{double x, y, z, r;}p[maxn];
struct node
{
int u, v;
double len;
friend bool operator < (node a, node b)
{
return a.len > b.len;
}
};
int f[maxn];
double Len(point a, point b)//求两点间的距离
{
double x = a.x - b.x;
double y = a.y - b.y;
double z = a.z - b.z;
double l = sqrt(x*x+y*y+z*z)-a.r-b.r;
if(l < 0)l = 0;
return l;
}
int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
}
int main()
{
int N;
while(scanf("%d", &N) != EOF && N)
{
int i, j;
node s;
priority_queue<node>Q;
for(i=1; i<=N; i++)
{
scanf("%lf%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z, &p[i].r);
f[i] = i;
}
for(i=1; i<=N; i++)
for(j=i+1; j<=N; j++)
{
s.u = i, s.v = j;
s.len = Len(p[i], p[j]);
Q.push(s);
}
double ans = 0;
while(Q.size())
{
s = Q.top();Q.pop();
int u = Find(s.u), v = Find(s.v);
if(u != v)
{
f[u] = v;
ans += s.len;
}
}
printf("%.3f\n", ans);
}
return 0;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;
#define maxn 105
struct point{double x, y, z, r;}p[maxn];
struct node
{
int u, v;
double len;
friend bool operator < (node a, node b)
{
return a.len > b.len;
}
};
int f[maxn];
double Len(point a, point b)//求两点间的距离
{
double x = a.x - b.x;
double y = a.y - b.y;
double z = a.z - b.z;
double l = sqrt(x*x+y*y+z*z)-a.r-b.r;
if(l < 0)l = 0;
return l;
}
int Find(int x)
{
if(f[x] != x)
f[x] = Find(f[x]);
return f[x];
}
int main()
{
int N;
while(scanf("%d", &N) != EOF && N)
{
int i, j;
node s;
priority_queue<node>Q;
for(i=1; i<=N; i++)
{
scanf("%lf%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z, &p[i].r);
f[i] = i;
}
for(i=1; i<=N; i++)
for(j=i+1; j<=N; j++)
{
s.u = i, s.v = j;
s.len = Len(p[i], p[j]);
Q.push(s);
}
double ans = 0;
while(Q.size())
{
s = Q.top();Q.pop();
int u = Find(s.u), v = Find(s.v);
if(u != v)
{
f[u] = v;
ans += s.len;
}
}
printf("%.3f\n", ans);
}
return 0;
}
相关文章推荐
- Unique Binary Search Trees II
- CodeForces#313 D. Equivalent Strings
- HDU 4069 Squiggly Sudoku
- IOS UIFont可选字体
- Scala 深入浅出实战经典 第39讲:ListBuffer、ArrayBuffer、Queue、Stack操作代码实战
- 最长公共子序列 (LCS,longest common subsequence problem) 动态规划
- LightOJ 1048 - Conquering Keokradong 【二分】
- Adjust color space Constrast,Brightness,Hue,Saturation in YCbCr format.
- HDU 5301 Buildings
- HDU 1047--Integer Inquiry 【大数】
- 黑马程序员——Java基础---GUI
- HDU 5297 Y sequence
- UITabBar和UINavigation碰到的几个问题 (系统颜色)
- 使用phpQuery实现批量文件处理
- Weka连接数据库出现No suitable driver found for jdbc:mysql://localhost/dbname
- 150723培训心得(queue)
- HDOJ Integer Inquiry (大数累加)
- HDU 1047 Integer Inquiry
- 给EasyUi的Form添加自动填充部分输入框的方法
- NGUI ScrollView动态添加和删除对象。