BZOJ 1013 JSOI2008 球形空间产生器 高斯消元

原题链接:

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

Description

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

Input

第一行是一个整数n(1<=N=10)。接下来的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

提示:给出两个定义:1、 球心:到球面上任意一点距离都相等的点。2、 距离:设两个n为空间上的点A, B的坐标为(a1, a2, …, an), (b1, b2, …, bn),则AB的距离定义为:dist = sqrt( (a1-b1)^2 + (a2-b2)^2 + … + (an-bn)^2 )

——————————————————————————————————————————————————————

题意概述:
·给出一个N维空间中的球上N+1个点的坐标,求这个球的球心坐标。
·N<=10,每个点坐标的绝对值不超过20000.

分析:
·推了一波式子可以发现利用N+1个点各自到球心的距离相等,把球心距离设出来之后可以得到N个互不相关的线性N元方程组,高斯消元上啊!!!(好吧我承认我就是想打个板子)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<set>
 9 #include<map>
10 #include<vector>
11 #include<cctype>
12 using namespace std;
13 const int maxn=15;
14 const double eps=1e-8;
15 
16 int N;
17 double P[maxn][maxn],A[maxn][maxn],B[maxn],X[maxn];
18 
19 void data_in()
20 {
21     scanf("%d",&N);
22     for(int i=1;i<=N+1;i++)
23     for(int j=1;j<=N;j++) scanf("%lf",&P[i][j]);
24 }
25 void Gauss(double a[maxn][maxn],double *b,double *x)
26 {
27     for(int i=1;i<=N;i++){
28         int tmp=i;
29         for(int j=i+1;j<=N;j++)
30             if(fabs(a[tmp][i])<fabs(a[j][i])) tmp=j;
31         for(int k=i;k<=N;k++) swap(a[i][k],a[tmp][k]);
32         swap(b[i],b[tmp]);
33         for(int j=i+1;j<=N;j++){
34             double t=a[j][i]/a[i][i];
35             for(int k=i;k<=N;k++) a[j][k]-=t*a[i][k];
36             b[j]-=t*b[i];
37         }
38     }
39     for(int i=N;i>=1;i--){
40         x[i]=b[i]/a[i][i];
41         for(int j=i-1;j>=1;j--) b[j]-=a[j][i]*x[i];
42     }
43 }
44 void work()
45 {
46     for(int i=1;i<=N;i++)
47     for(int j=1;j<=N;j++){
48         A[i][j]=2*(P[1][j]-P[1+i][j]);
49         B[i]+=P[1][j]*P[1][j]-P[1+i][j]*P[1+i][j];
50     }
51     Gauss(A,B,X);
52     for(int i=1;i<N;i++) printf("%.3f ",X[i]);
53     printf("%.3f
",X[N]);
54 }
55 int main()
56 {
57     data_in();
58     work();
59     return 0;
60 }
原文地址:https://www.cnblogs.com/KKKorange/p/8605280.html