BZOJ 1013: [JSOI2008]球形空间产生器sphere 高斯消元

时间:2023-03-09 08:27:44
BZOJ 1013: [JSOI2008]球形空间产生器sphere 高斯消元

1013: [JSOI2008]球形空间产生器sphere

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=1013

Description

有一个球形空间产生器能够在n维空间中产生一个坚硬的球体。现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁这个球形空间产生器。

Input

第一行是一个整数,n。接下来的n+1行,每行有n个实数,表示球面上一点的n维坐标。每一个实数精确到小数点后6位,且其绝对值都不超过20000。

Output

有且只有一行,依次给出球心的n维坐标(n个实数),两个实数之间用一个空格隔开。每个实数精确到小数点后3位。数据保证有解。你的答案必须和标准输出一模一样才能够得分。

Sample Input

2
0.0 0.0
-1.0 1.0
1.0 0.0

Sample Output

0.500 1.500

HINT

数据规模:

对于40%的数据,1<=n<=3

对于100%的数据,1<=n<=10

提示:给出两个定义:

1、 球心:到球面上任意一点距离都相等的点。

2、 距离:设两个n为空间上的点A, B的坐标为(a1, a2, …, an), (b1, b2, …, bn),则AB的距离定义为:dist = sqrt( (a1-b1)^2 + (a2-b2)^2 + … + (an-bn)^2 )

题意

题解

高斯消元

假设圆心坐标为(x1,x2,x3.....xn)

那:(a1-x1)^2+(a2-x2)^2.......+(an-xn)^2=r^2

(b1-x1)^2+(b2-x2)^2.......+(bn-xn)^2=r^2

两个方程相减得到:(a1-b1)x1+(a2-b2)x2......(an-bn)xn=(a1^2-b1^2)+.......+(an^2-bn^2)/2

然后高斯消元求解就好了

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 1000100
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//**************************************************************************************
int n;
double f[];
double a[][];
void gauss()
{
int now,to;double t;
for(int i=;i<=n;i++)
{
now=i;
for(to=i+;to<=n;to++)
if(abs(a[to][i])>abs(a[now][i])) now=to;
if(now!=i)
{
for(int j=;j<=n+;j++)
swap(a[i][j],a[now][j]);
}
for(int j=i+;j<=n;j++)
{
double tmp=a[j][i]/a[i][i];
a[j][i]=;
for(int k=i+;k<=n+;k++)
a[j][k]-=tmp*a[i][k];
}
}
for(int i=n;i;i--)
{
for(int j=n;j>i;j--)
a[i][n+]-=a[j][n+]*a[i][j];
a[i][n+]/=a[i][i];
}
for(int i=;i<n;i++)
printf("%.3lf ",a[i][n+]);
printf("%.3lf",a[n][n+]);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lf",&f[i]);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
double t;
scanf("%lf",&t);
a[i][j]=*(t-f[j]);
a[i][n+]+=t*t-f[j]*f[j];
}
}
gauss();
return ;
}