标签:const str syn bool col sizeof 高斯消元 tchar first
程序使用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 "\n" #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\n",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; } */
标签:const str syn bool col sizeof 高斯消元 tchar first
原文地址:https://www.cnblogs.com/033000-/p/10754332.html