简单的两圆关系

题 目 地 址 \color{red}题目地址
有一定总结性的题目,所以我记下来了。
题意就是对于三维中的n个球找到一条最小生成树,相碰或者想包含的小球视为可以直接到达。所以这样的情况直接让边长为 0 0 0即可。那么怎么判断这样的情况呢。
d d d为圆心距, R R R为大圆半径, r r r为小圆半径
d = R − r d=R-r d=Rr时,两圆内切
R − r &lt; d &lt; R + r R-r&lt;d&lt;R+r Rr<d<R+r时,两圆相交
d = R + r d=R+r d=R+r时,两圆外切
d &gt; R + r d&gt;R+r d>R+r时,两圆不相交
那么直接求就可以了。唯一需要注意的是: G + + G++ G++提交的时候double输出应该用 % f \%f %f
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<cctype>
#include<string>
#include<cmath>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define eps 1e-8
using namespace std;

int n;
const int maxn=500050;
const int maxm=1000100;

struct circle{
    double cx,cy,cz;
    double r;
    circle(){}
    circle(double _cx,double _cy,double _cz,double _r):cx(_cx),cy(_cy),cz(_cz),r(_r){}
}A[1010];

struct Edge{
    int from,to;double dist;
    Edge(){}
    Edge(int _from,int _to,double _dist):from(_from),to(_to),dist(_dist){}
};

Edge ed[maxm];int he[maxn],ne[maxm],etop=1;
void insert(int u,int v,double w){ed[etop]=Edge(u,v,w);ne[etop]=he[u];he[u]=etop++;}

bool vis[maxn];double minc[maxn];double ans=0;
priority_queue<pair<double,int>,vector<pair<double,int> >,greater<pair<double,int> > >q;

void prim(){
    memset(vis,0,sizeof(vis));
    memset(minc,0x7f,sizeof(minc));
    q.push(make_pair(0.0,1));
    while(!q.empty()){
        double c=q.top().first;
        int now=ed[q.top().second].to;
        q.pop();
        if(!vis[now]){
            vis[now]=true;
            ans+=c;
            for(int i=he[now];i;i=ne[i]){
                Edge &e = ed[i];
                if(minc[e.to]>e.dist&&!vis[e.to]){
                    minc[e.to]=e.dist;
                    q.push(make_pair(e.dist,i));
                }
            }
        }
    }
}

double cal(circle a,circle b){
    double d=(a.cx-b.cx)*(a.cx-b.cx)+(a.cy-b.cy)*(a.cy-b.cy)+(a.cz-b.cz)*(a.cz-b.cz);
    d=sqrt(d);
    if(d-(a.r+b.r)>eps)return d-(a.r+b.r);
    else return 0.0;
}

int main(){
    while(cin>>n){
        if(n==0)break;
        memset(he,0,sizeof(he));
        memset(ne,0,sizeof(ne));
        etop=1,ans=0;
        double x,y,z,r;
        FOR(i,1,n){
            scanf("%lf%lf%lf%lf",&x,&y,&z,&r);
            A[i]=circle(x,y,z,r);
        }
        FOR(i,1,n)FOR(j,i+1,n)if(i!=j){
            double tmp=cal(A[i],A[j]);
            insert(i,j,tmp);
            insert(j,i,tmp);
        }
        prim();
        printf("%.3lf\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
两个圆的关系可以分为以下几种情况: 1. 两个圆重合,即圆心距离为0,此时两个圆是同一个圆。 2. 两个圆相离,即圆心距离大于两个圆的半径之和,此时两个圆没有交点。 3. 两个圆相交,即圆心距离小于两个圆的半径之和,此时两个圆有交点。 4. 一个圆包含另一个圆,即一个圆的半径大于另一个圆的半径并且两个圆的圆心距离小于等于两个圆的半径之差,此时两个圆有交点,但是其中一个圆完全包含另一个圆。 以下是计算两个圆的关系的示例代码: ```python import math def circle_relation(x1, y1, r1, x2, y2, r2): # 计算两个圆的圆心距离 distance = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) # 判断两个圆的关系 if distance == 0: if r1 == r2: return "两个圆重合" else: return "两个圆同心,但是半径不同" elif distance > r1 + r2: return "两个圆相离" elif distance < abs(r1 - r2): if r1 > r2: return "一个圆包含另一个圆" else: return "一个圆被另一个圆包含" else: return "两个圆相交" ``` 使用示例: ```python print(circle_relation(0, 0, 5, 10, 0, 5)) # 两个圆相交 print(circle_relation(0, 0, 5, 20, 0, 5)) # 两个圆相离 print(circle_relation(0, 0, 5, 2, 0, 3)) # 一个圆包含另一个圆 print(circle_relation(0, 0, 5, 2, 0, 10)) # 一个圆被另一个圆包含 print(circle_relation(0, 0, 5, 0, 0, 5)) # 两个圆重合 print(circle_relation(0, 0, 5, 0, 0, 10)) # 两个圆同心,但是半径不同 ``` 输出结果: ``` 两个圆相交 两个圆相离 一个圆包含另一个圆 一个圆被另一个圆包含 两个圆重合 两个圆同心,但是半径不同 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值