首页 > 代码库 > BZOJ1013: [JSOI2008]球形空间产生器sphere

BZOJ1013: [JSOI2008]球形空间产生器sphere

传送门

高斯消元练习。

模板:

void Guass(){		int waited;	up(i,1,N){		waited=i;		up(j,i+1,N)if(fabs(M[j][i])>fabs(M[waited][i]))waited=j;		if(waited!=i)up(j,1,N+1)swap(M[i][j],M[waited][j]);		up(k,i+1,N){			db f=M[k][i]/M[i][i];			up(j,i,N+1)M[k][j]-=f*M[i][j];		}	}	down(i,N,1){		up(j,i+1,N)M[i][N+1]-=M[j][N+1]*M[i][j];		M[i][N+1]/=M[i][i];	}}

 具体实现:

//BZOJ 1013//by Cydiater//2016.11.2#include <iostream>#include <cstdlib>#include <cstdio>#include <queue>#include <map>#include <ctime>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <bitset>#include <set>#include <iomanip>using namespace std;#define ll long long#define db double#define up(i,j,n)		for(int i=j;i<=n;i++)#define down(i,j,n)		for(int i=j;i>=n;i--)#define cmax(a,b)		a=max(a,b)#define cmin(a,b)		a=min(a,b)const int MAXN=15;const int oo=0x3f3f3f3f;int N;db f[MAXN],M[MAXN][MAXN];namespace solution{	void init(){		scanf("%d",&N);		up(i,1,N)scanf("%lf",&f[i]);		up(i,1,N)up(j,1,N){			double tmp;scanf("%lf",&tmp);			M[i][j]=2*(tmp-f[j]);			M[i][N+1]+=tmp*tmp-f[j]*f[j];		}	}	void Guass(){		int waited;		up(i,1,N){			waited=i;			up(j,i+1,N)if(fabs(M[j][i])>abs(M[waited][i]))waited=j;			if(waited!=i)up(j,1,N+1)swap(M[i][j],M[waited][j]);			up(k,i+1,N){				db f=M[k][i]/M[i][i];				up(j,i,N+1)M[k][j]-=f*M[i][j];			}		}		down(i,N,1){			up(j,i+1,N)M[i][N+1]-=M[j][N+1]*M[i][j];			M[i][N+1]/=M[i][i];		}	}	void slove(){		Guass();	}	void output(){		up(i,1,N-1)printf("%.3lf ",M[i][N+1]);		printf("%.3lf\n",M[N][N+1]);	}}int main(){	freopen("input.in","r",stdin);	using namespace solution;	init();	slove();	output();	return 0;}

 

BZOJ1013: [JSOI2008]球形空间产生器sphere