Cod sursa(job #818687)

Utilizator rvnzphrvnzph rvnzph Data 17 noiembrie 2012 20:25:23
Problema Tunelul groazei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <iostream>
#include <stdio.h>
#include <iomanip>

#define NLen 256
#define EPS 0.001

using namespace std;

int cost[NLen][NLen];
int num[NLen][NLen];
double e[NLen][NLen];

int grad[NLen];
double tm[NLen];

int N;

inline void make_system()
{
	for(int i=1;i<N;++i)
	{
		e[i][i]=grad[i];
		
		for(int j=1;j<=N;++j)
			if(cost[i][j])
			{
				if(j!=N) e[i][j]-=num[i][j];
				e[i][N]+=cost[i][j];
			}
	}
}

inline void gauss()
{
	for(int i=1;i<N;++i)
	{
		if(e[i][i]!=1)
		{
			for(int j=i+1;j<=N;++j) e[i][j]/=e[i][i];
			e[i][i]=1;
		}
		
		for(int r=i+1;r<N;++r)
			if(e[r][i])
			{
				for(int c=i+1;c<=N;++c)
					e[r][c]-=e[r][i]*e[i][c];
				e[r][i]=0;
			}
	}
	
	for(int i=N-1;i>0;--i)
	{
		tm[i]=e[i][N];
		for(int j=i+1;j<N;++j)
			tm[i]-=tm[j]*e[i][j];
	}
}

int main()
{
	freopen("tunel.in","r",stdin);
	freopen("tunel.out","w",stdout);
	
	int M,x,y,c;
	
	cin>>N>>M;
	
	for(int i=1;i<=M;++i)
	{
		scanf("%d%d%d",&x,&y,&c);
		
		++num[x][y];
		++num[y][x];
		
		cost[x][y]+=c;
		cost[y][x]+=c;
		
		++grad[x];
		++grad[y];
	}
	
	make_system();
	gauss();
	
	cout<<fixed<<setprecision(3)<<tm[1]<<'\n';
	
	return 0;
}