poj 2031
每日一題 9.28.2020
題意:
就是給出三維座標系上的一些球的球心座標和其半徑,搭建通路,使得他們能夠相互連通。如果兩個球有重疊的部分則算為已連通,無需再搭橋。求搭建通路的最小費用(費用就是邊權,就是兩個球面之間的距離)。
解題思路:
邊權 = AB球面距離 = A球心到B球心的距離 – A球半徑 – B球半徑
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include<math.h>
using namespace std;
struct cir
{
double x,y,z,r;
}p[111];
struct node
{
int u;
int v;
double w;
}r[10010];
int f[111];
double dis (cir a, cir b) {
double xx = a.x-b.x, yy = a.y-b.y, zz = a.z-b.z;
double d = sqrt (xx*xx + yy*yy + zz*zz);
if (d <= a.r+b.r)
return 0;
return d - a.r - b.r;
}
bool cmp(node a,node b)
{
return a.w<b.w;
}
int Find(int x)
{
if(f[x]!=x)
Find(f[x]);
else return x;
}
int join(int x,int y)
{
int rot1=Find(x);
int rot2=Find(y);
if(rot1!=rot2)
{
f[rot1]=rot2;
return 1;
}
return 0;
}
int main()
{
int n;
while(cin>>n&&n)
{
for(int i=1;i<=n;i++)
{
cin>>p[i].x>>p[i].y>>p[i].z>>p[i].r;
f[i]=i;
}
int len=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
r[len].u=i;
r[len].v=j;
r[len++].w=dis(p[i],p[j]);
}
}
sort(r,r+len,cmp);
double num=0.0;int coun=0;
for(int i=0;i<len;i++)
{
if(join(r[i].u,r[i].v))
{
num+=r[i].w;
coun++;
}
if(coun==n-1) break;
}
printf("%.3f\n",num);
}
}
相關文章
- 【題解】CF2031
- 2031資料庫概述資料庫
- CF2031D 題解
- CF2031F Penchick and Even Medians
- Solution - Codeforces 2031F Penchick and Even Medians
- poj 3461
- POJ 1089 Intervals
- POJ 3414 Pots
- poj3417
- POJ 2975 Nim
- poj 3278 BFS
- POJ3259-WormholesWorm
- POJ3414-Pots
- POJ 1442 Black Box
- POJ 2799 IP Networks
- 【BFS】poj 3414 Pots
- Network(POJ-1144)
- POJ 2553 The Bottom of a Graph
- POJ 1861 Network (Kruskal)
- Apple Catching POJ - 2385APP
- POJ3126-Prime Path
- POJ1426-Find The Multiple
- POJ2251 Dungeon MasterAST
- Dungeon Master(POJ-2251)AST
- POJ 1611 The Suspects 圖論圖論
- POJ 3267 The Cow Lexicon(dp)
- POJ3278 Catch That Cow
- POJ - 3090 Visible Lattice Points
- POJ 2355 Railway Ticket problemAI
- Road Construction(POJ-3352)Struct
- Redundant Paths(POJ-3177)
- The Cow Prom(POJ-3180)
- Network of Schools(POJ-1236)
- POJ3414 Pots【BFS】
- POJ 3071 Football(概率DP)
- [poj1275][Cashier Employment]
- POJ - 1125 Stockbroker Grapevine(Java)Java
- POJ 2184 (01揹包)