ZOJ 3645 (高斯消元)

BiliBili Time Limit: 2 Seconds       Memory Limit: 65536 KB

Shirai Kuroko is a Senior One student. Almost everyone in Academy City have super powers, and Kuroko is good at using it. Her ability is "Teleporting", which can make people to transfer in the eleven dimension, and it shows like moving instantly in general people's eyes.

ZOJ 3645 (高斯消元)_第1张图片

In fact, the theory of the ability is simple. Each time, Kuroko will calculate the distance between some known objects and the destination in the eleven dimension so that Kuroko can get the coordinate of the destination where she want to go and use her ability.

Now we have known that the coordinate of twelve objects in the eleven dimension Vi = (Xi1,Xi2, ... ,Xi11), and 1 <= i <= 12. We also known that the distance Di between the destination and the object. Please write a program to calculate the coordinate of the destination. We can assume that the answer is unique and any four of the twelve objects are not on the same planar.

Input

The first line contains an integer T, means there are T test cases. For each test case, there are twelve lines, each line contains twelve real numbers, which means Xi1,Xi2, ... ,Xi11 and Di. T is less than 100.

Output

For each test case, you need to output eleven real numbers, which shows the coordinate of the destination. Round to 2 decimal places.

Sample Input

1
1.0 0 0 0 0 0 0 0 0 0 0 7.0
0 1.0 0 0 0 0 0 0 0 0 0 7.0
0 0 1.0 0 0 0 0 0 0 0 0 7.0
0 0 0 1.0 0 0 0 0 0 0 0 7.0
0 0 0 0 1.0 0 0 0 0 0 0 7.0
0 0 0 0 0 1.0 0 0 0 0 0 7.0
0 0 0 0 0 0 1.0 0 0 0 0 7.0
0 0 0 0 0 0 0 1.0 0 0 0 7.0
0 0 0 0 0 0 0 0 1.0 0 0 7.0
0 0 0 0 0 0 0 0 0 1.0 0 7.0
0 0 0 0 0 0 0 0 0 0 1.0 7.0
7.0 0 0 0 0 0 0 0 0 0 0 11.0

Sample Output

-2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00 -2.00
 
 
题意是在11维空间,已知某个点到其他12个点的距离(这12个点的坐标已知),求出这个点的坐标。
很简单的高斯消元,消去2次项刚好是11个方程11个未知数的线性方程组。
坑点是结果要加上eps防止-0.00这样的答案的出现。
 
 
#include <bits/stdc++.h>
using namespace std;
#define maxn 211
#define eps 1e-9

double a[maxn][maxn], x[maxn];
double mp[15][15];
int equ, var;

int Gauss () {
    int i, j, k, col, max_r;
    for (k = 0, col = 0; k < equ && col < var; k++, col++) {
        max_r = k;
        for (int i = k+1; i < equ; i++) { //找到最大的数所在的行
            if (fabs (a[i][col]) > fabs (a[max_r][col]))
                max_r = i;
        }
        if (k != max_r) { //交换行
            for (int j = col; j <= var; j++) {
                swap (a[k][j], a[max_r][j]);
            }
        }
        for (int i = k+1; i < equ; i++) { //消去
            if (fabs (a[i][col]) > eps) {
                double tmp = -a[i][col]/a[k][col];
                for (int j = col; j <= var; j++) {
                    a[i][j] += tmp*a[k][j];
                }
            }
        }
    }
    for (int i = equ-1; i >= 0; i--) { //回代
        double tmp = a[i][var];
        for (int j = i+1; j < var; j++) {
            tmp -= x[j]*a[i][j];
        }
        x[i] = tmp/a[i][i];
    }
    return 1;
}

int main () {
    //freopen ("in", "r", stdin);
    int kase = 0, t;
    scanf ("%d", &t);
    while (t--) {
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 12; j++)
                cin >> mp[i][j];
        }
        memset (a, 0, sizeof a);
        equ = 11, var = 11;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                a[i][j] -= 2*mp[i][j];
                a[i][j] += 2*mp[11][j];
                a[i][11] -= mp[i][j]*mp[i][j];
                a[i][11] += mp[11][j]*mp[11][j];
            }
            a[i][11] += mp[i][11]*mp[i][11] - mp[11][11]*mp[11][11];
        }
        Gauss ();
        for (int i = 0; i < 11; i++) {
            printf ("%.2f%c", x[i]+eps, (i == 10 ? '\n' : ' '));
        }
    }
    return 0;
}
//-2.00 -2.00



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