Cod sursa(job #239629)

Utilizator SleepyOverlordPatcas Csaba SleepyOverlord Data 5 ianuarie 2009 09:19:18
Problema Reconst Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.18 kb
#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <assert.h>

using namespace std;

int n,m,ind;
char buf[30];
vector < vector <char> > a;
vector <short> sol;

int readn()
{
	int ans=0;
	bool negate=false;
	while (buf[ind]=='-' || isdigit(buf[ind]))
	{
		if (buf[ind]=='-') negate=true;
		else ans=ans*10+buf[ind]-'0';
		++ind;
	}
	++ind;
	if (negate) ans=-ans;
	return ans;
}

void GaussJordan()
{
	vector <bool> was(m+1);
	for (int w=1;w<=n;++w)
		for (int q=1;q<=m;++q)
			if (!was[q] && a[q][w]==1)
			{
				was[q]=true;
				for (int i=1;i<=m;++i)
				{
					if (q==i || a[i][w]==0) continue;
					int szorzo=a[i][w]/a[q][w];
					for (int j=1;j<=n+1;++j) a[i][j]-=a[q][j]*szorzo;
				}
			}
}

void solve()
{
	bool found;
	int solved=0;
	vector <bool> done(n+1);
	while (solved<n)
	{
		found=true;
		while (found)
		{
			found=false;
			for (int q=1;q<=m;++q)
			{
				int n1=0, col;
				for (int w=1;w<=n;++w) 
					if (a[q][w]!=0)
					{
						++n1;
						col=w;
					}
				if (n1==1 && (a[q][n+1]%a[q][col])==0)
				{
					found=true;
					done[col]=true;
					++solved;
					assert((a[q][n+1]%a[q][col])==0);
					sol[col]=a[q][n+1]/a[q][col];
					for (int i=1;i<=m;++i)
						if (a[i][col])
						{
							a[i][n+1]-=a[i][col]*sol[col];
							a[i][col]=0;
						}
				}
			}
		}
		int col=0;
		for (int q=1;q<=m;++q)
		{
			for (int w=1;w<=n;++w)
				if (a[q][w]!=0 && !done[w])
				{
					col=w;
					break;
				}
			if (col) break;
		}
		done[col]=true;
		++solved;
		sol[col]=0;
		for (int i=1;i<=m;++i)
			if (a[i][col])
			{
				a[i][n+1]-=a[i][col]*sol[col];
				a[i][col]=0;
			}
	}
	for (int q=1;q<=n;++q) assert(done[q]);
}

int main()
{
	FILE* fin=fopen("reconst.in","r");
	fscanf(fin,"%d%d\n",&n,&m);

	a.resize(m+1,vector <char> (n+2));
	for (int q=1;q<=m;++q)
	{
		fgets(buf,30,fin);
		ind=0;
		int x=readn(),y=readn(),z=readn();
		for (int w=x;w<=y;++w) a[q][w]=1;
		a[q][n+1]=z;
	}
	fclose(fin);

	GaussJordan();

	sol.resize(n+1);
	solve();

	FILE* fout=fopen("reconst.out","w");
	for (int q=1;q<=n;++q) fprintf(fout,"%d ",sol[q]);
	fclose(fout);
	return 0;
}