码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-11-03 09:43:46      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:time   max   double   false   jsoi2008   mat   ++   pac   bzoj1013   

传送门

高斯消元练习。

模板:

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

标签:time   max   double   false   jsoi2008   mat   ++   pac   bzoj1013   

原文地址:http://www.cnblogs.com/Cydiater/p/6025236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!