zoukankan      html  css  js  c++  java
  • ZOJ 3645 BiliBili(高斯消元)

    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.

    Railgun_Kuroko.jpg

    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, ... ,Xi11and DiT 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.

    题目大意:11维度上有一个未知的点,现在已知12个点的坐标和他们到这个未知的点的距离,求这个未知的点的坐标。

    思路:设未知的点为(p1, p2, ……, pn)

    那么对于每个已知点,列方程(xi1 - p1)^2 + (xi2 - p2)^2 + …… + (xin - pn)^2 = Di^2

    然后,对于前11个方程,减去第12个方程,就能得到一个线性方程组。

    然后高斯消元解即可。

    PS:我的代码不加那个EPS会跪,我觉得以后没事还是把它加上吧……

    代码(0MS):

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cmath>
     6 using namespace std;
     7 
     8 const double EPS = 1e-12;
     9 const int MAXN = 15;
    10 
    11 double mat[MAXN][MAXN];
    12 int n = 11, T;
    13 
    14 inline int sgn(double x) {
    15     return (x > EPS) - (x < -EPS);
    16 }
    17 
    18 void guess_eliminatioin() {
    19     for(int i = 0; i < n; ++i) {
    20         int r = i;
    21         for(int j = i + 1; j < n; ++j)
    22             if(fabs(mat[j][i]) > fabs(mat[r][i])) r = j;
    23         if(sgn(mat[r][i]) == 0) continue;
    24         if(r != i) for(int j = 0; j <= n; ++j) swap(mat[r][j], mat[i][j]);
    25         for(int j = n; j >= i; --j)
    26             for(int k = i + 1; k < n; ++k) mat[k][j] -= mat[k][i] / mat[i][i] * mat[i][j];
    27     }
    28     for(int i = n - 1; i >= 0; --i) {
    29         for(int j = i + 1; j < n; ++j)
    30             mat[i][n] -= mat[j][n] * mat[i][j];
    31         mat[i][n] /= mat[i][i];
    32     }
    33 }
    34 
    35 int main() {
    36     scanf("%d", &T);
    37     while(T--) {
    38         for(int i = 0; i <= n; ++i)
    39             for(int j = 0; j <= n; ++j) scanf("%lf", &mat[i][j]);
    40         for(int i = 0; i < n; ++i) {
    41             mat[i][n] = mat[i][n] * mat[i][n] - mat[n][n] * mat[n][n];
    42             for(int j = 0; j < n; ++j) {
    43                 mat[i][n] -= mat[i][j] * mat[i][j] - mat[n][j] * mat[n][j];
    44                 mat[i][j] = -2 * mat[i][j] + 2 * mat[n][j];
    45             }
    46         }
    47         guess_eliminatioin();
    48         for(int i = 0; i < n - 1; ++i) printf("%.2f ", mat[i][n] + EPS);
    49         printf("%.2f
    ", mat[n - 1][n] + EPS);
    50     }
    51 }
    View Code
  • 相关阅读:
    [译]在Python中如何使用额enumerate 和 zip 来迭代两个列表和它们的index?
    [译]如何去除Git的unstaged的文件提示“old mode 100755 new mode 100644”?
    [译]在SQL查询中如何映射(替换)查询的结果?
    [总结]《敏捷软件开发: 原则、模式与实践》一次编程实践
    [书摘]《敏捷软件开发: 原则、模式与实践》第一部分:敏捷开发
    [译]Python
    [问题解决]Python locale error: unsupported locale setting
    [持续补充]开发过程中常见bug查找思路
    [译]如何比较同一分支上的不同commit的代码区别?
    [整理]如何切换到远程分支
  • 原文地址:https://www.cnblogs.com/oyking/p/3613312.html
Copyright © 2011-2022 走看看