zoukankan      html  css  js  c++  java
  • 高斯消元

    装备购买HYSBZ - 4004 

    程序使用long double 这一类型,相比于double,long double 的精度更高,但运算速度稍微慢点

    这是一道及其典型的高斯消元,应对的也是各种情况,有解的,没解的,已经未知数个数比方程数多或者少或者相等

    可以当做模板

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<map>
    #include<set>
    #include<list>
    #include<ctime>
    #include<ctype.h>
    #include<bitset>
    #include<algorithm>
    #include<numeric> //accumulate
    #define endl "
    "
    #define fi first
    #define se second
    #define FOR(i,s,t) for(int i=(s);i<=(t);++i)
    #define mem(a,b) memset(a,b,sizeof(a))
    #define debug(x) printf("%d
    ",x)
    using namespace std;
    const int maxn=505;
    long double a[maxn][maxn];
    int c[maxn];
    int n,m;
    const long double eps=1e-8;
    struct rec
    {
        int i;
        bool operator<(const rec& rhs)const
        {
            return c[i]<c[rhs.i];
        }
    };
    set<rec> S;
    int main()
    {
    
        //cin.tie(0);
        //cout.tie(0);
        //ios_base::sync_with_stdio(false);
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                cin>>a[i][j];
            }
        }
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&c[i]);
        }
        int dim=0,ans=0,zh=min(n,m);
        for(int i=1; i<=zh; i++)
        {
            S.clear();
            for(int j=dim+1; j<=n; j++)
            {
                if(fabs(a[j][i])>eps)
                {
                    S.insert((rec){j});
                }
            }
            if(S.empty())
            {
                continue;
            }
            dim++;
            int j=S.begin()->i;
            ans+=c[j];
            for(int k=dim; k<=m; k++)
            {
                swap(a[dim][k],a[j][k]);
            }
            swap(c[dim],c[j]);
            for(j=1; j<=n; j++)
            {
                if(i==j||fabs(a[j][i])<eps)
                    continue;
                long double rate=a[j][i]/a[i][i];
                for(int k=i; k<=m; k++)
                {
                    a[j][k]-=rate*a[dim][k];
                }
            }
        }
        printf("%d %d",dim,ans);
    }
    
    /*
    void read()
    {
    
        char c = getchar();
        int x = 0;
        for (; (c < 48 || c>57); c = getchar());
        for (; c > 47 && c < 58; c = getchar())
        {
            x = (x << 1) + (x << 3) + c - 48;
        }
        return x;
    }
    */
  • 相关阅读:
    form 编译命令
    Form文件夹开发步骤
    使用View为Data Source的Form开发要点
    spring2.0包说明【转】
    Zero to One读后感
    Fourth glance in Go
    Third glance in Go
    Second glance in Go
    First glance in Go
    MongoDB 安装
  • 原文地址:https://www.cnblogs.com/033000-/p/10754332.html
Copyright © 2011-2022 走看看