Cod sursa(job #326456)

Utilizator AndreyPAndrei Poenaru AndreyP Data 25 iunie 2009 11:01:54
Problema Tunelul groazei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
using namespace std;
#define N 260
#define pb push_back
#define fs first
#define sc second
#define mp make_pair
int n,deg[N];
double eq[N][N];
vector< pair<int,int> > a[N];
inline double modul(double x)
{
	if(x<0)
		return -x;
	return x;
}
inline void citire()
{
	int m,x,y,z;
	scanf("%d%d",&n,&m);
	for(int i=0; i<m; ++i)
	{
		scanf("%d%d%d",&x,&y,&z);
		a[x].pb(mp(y,z));
		a[y].pb(mp(x,z));
		++deg[x];
		++deg[y];
	}
}
inline void rezolva()
{
	for(int i=1; i<n; ++i)
	{
		for(size_t j=0,lim=a[i].size(); j<lim; ++j)
		{
			eq[i][a[i][j].fs]+=1;
			eq[i][n+1]-=(double)a[i][j].sc;
		}
		eq[i][i]=-(double)deg[i];
	}
	for(int i=1; i<=n+1; ++i)
		eq[n][i]=0;
	for(int rand=1,col=n-1; col>0; --col,++rand)
	{
		int k=rand;
		double aux=modul(eq[rand][col]);
		for(int i=rand+1; i<=n; ++i)
		{
			if(modul(eq[i][col])>aux)
			{
				aux=modul(eq[i][col]);
				k=i;
			}
		}
		if(aux==0)
			exit(4);
		if(k!=rand)
		{
			for(int i=1; i<=n+1; ++i)
				swap(eq[rand][i],eq[k][i]);
		}
		for(int i=rand+1; i<=n; ++i)
		{
			if(eq[i][col]==0)
				continue;
			aux=-eq[i][col]/eq[rand][col];
			for(int j=1; j<=n+1; ++j)
				eq[i][j]+=aux*eq[rand][j];
			eq[i][col]=0;
		}
	}
	printf("%lf\n",eq[n-1][n+1]/eq[n-1][1]);
}
int main()
{
	freopen("tunel.in","r",stdin);
	freopen("tunel.out","w",stdout);
	citire();
	rezolva();
	return 0;
}