Cod sursa(job #818684)

Utilizator rvnzphrvnzph rvnzph Data 17 noiembrie 2012 20:19:29
Problema Tunelul groazei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <iomanip>

#define NLen 256
#define EPS 0.001

using namespace std;

vector <int> g[NLen];

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

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

int N;

inline double abs(double x)
{
	if(x<0) x*=-1;
	
	return x;
}

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

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(abs(e[r][i])>=EPS)
			{
				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)
	{
		cin>>x>>y>>c;
		
		g[x].push_back(y);
		g[y].push_back(x);
		
		++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;
}