【BZOJ1013】[JSOI2008]球形空间产生器sphere【高斯消元】

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

将两个点的方程做差可以得到一个线性方程,就可以高斯消元了。


/* Footprints In The Blood Soaked Snow */
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

typedef double DB;

const int maxn = 12;
const DB eps = 1e-5;

int n, m;
DB p[maxn][maxn], A[maxn][maxn];

void gauss() {
	for(int i = 1; i <= n; i++) {
		if(fabs(A[i][i]) < eps) for(int j = i + 1; j <= n; j++) if(fabs(A[j][i]) > eps) {
			for(int k = 1; k <= m; k++) swap(A[j][k], A[i][k]);
			break;
		}
		for(int j = 1; j <= n; j++) if(i ^ j) {
			DB t = A[j][i] / A[i][i];
			for(int k = 1; k <= m; k++) A[j][k] -= t * A[i][k];
		}
	}
	for(int i = 1; i <= n; i++) A[i][m] /= A[i][i];

	printf("%.3lf", A[1][m]);
	for(int i = 2; i <= n; i++) printf(" %.3lf", A[i][m]);
}	

int main() {
	scanf("%d", &n); m = n + 1;

	for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++)
		scanf("%lf", &p[i][j]);

	for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) {
		A[i][j] = 2.0 * (p[m][j] - p[i][j]);
		A[i][m] += p[m][j] * p[m][j] - p[i][j] * p[i][j];
	}		

	gauss();
	return 0;
}


你可能感兴趣的:(高斯消元)