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

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

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 6139  Solved: 3190
[Submit][Status][Discuss]

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 个等式,然后解出每个变量就好,也就是高斯消元。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int maxn = 20 + 10;
const int maxm = 3e5 + 10;
const int mod = 100003;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}

double a[maxn][maxn];

void Gauess(int n){
  for(int i = 0; i < n; ++i){
    int r = i;
    for(int j = i+1; j < n; ++j)
      if(fabs(a[j][i]) > fabs(a[r][i]))  r = j;
      if(r != i)  for(int j = 0; j <= n; ++j)  swap(a[r][j], a[i][j]);

      for(int k = i+1; k < n; ++k){
        double f = a[k][i] / a[i][i];
        for(int j = i; j <= n; ++j)  a[k][j] -= f * a[i][j];
      }
  }
  for(int i = n-1; i >= 0; --i){
    for(int j = i+1; j < n; ++j)
      a[i][n] -= a[j][n] * a[i][j];
    a[i][n] /= a[i][i];
  }
}

double X[maxn];

int main(){
  scanf("%d", &n);
  for(int i = 0; i < n; ++i)  scanf("%lf", X + i);
  for(int i = 0; i < n; ++i)
    for(int j = 0; j < n; ++j){
      double x;  scanf("%lf", &x);
      a[i][j] = 2. * (X[j] - x);
      a[i][n] += sqr(X[j]) - sqr(x);
    }
  Gauess(n);
  for(int i = 0; i < n; ++i)  printf("%.3f%c", a[i][n], i == n-1 ? '
' : ' ');
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/7810469.html