Cod sursa(job #351868)

Utilizator andrei-alphaAndrei-Bogdan Antonescu andrei-alpha Data 29 septembrie 2009 18:11:50
Problema Tunelul groazei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <cstdio>
#define FOR(i,a,b) for(int i=a;i<=b;++i)

double C[270][270],A[270];
int N,M;

void scan()
{
	freopen("tunel.in","r",stdin);
	freopen("tunel.out","w",stdout);
	
	scanf("%d%d",&N,&M);
	--N;
	
	int x,y,c;
	FOR(i,1,M)
	{
		scanf("%d%d%d",&x,&y,&c);
		
		++C[x][y],++A[x]; 
		++C[y][x],++A[y]; 
		C[x][0] += (double)c;
		C[y][0] += (double)c;
	}
}

void solve()
{
	FOR(i,1,N)
	FOR(j,0,N)
		C[i][j] /= A[i];
	
	FOR(i,2,N)
	FOR(j,1,N)
	FOR(k,0,N)
		if(j != i && k != i)
			C[j][k] += C[i][k] * 1 / (1 - C[i][i]) * C[j][i];
	
	printf("%lf\n",(1.0 / (1.0 - C[1][1]) ) * C[1][0]);
}

int main()
{
	scan();
	
	solve();
	return 0;
}