Cod sursa(job #109458)

Utilizator alextheroTandrau Alexandru alexthero Data 25 noiembrie 2007 11:11:16
Problema Tunelul groazei Scor 0
Compilator cpp Status done
Runda preONI 2008, Runda 1, Clasele 11-12 Marime 0.98 kb
#include <stdio.h>
#include <vector>

#define testMax 1000
#define pb push_back
#define nmax 256

using namespace std;
typedef pair<int, int> ii;

int n, m, nx, ny, cst, a[testMax + 5][nmax], b[testMax + 5][nmax];
vector <ii> e[nmax];

int main()
{
	freopen("tunel.in", "r", stdin);
	freopen("tunel.out", "w", stdout);

	scanf("%d%d", &n, &m);
	for(int i = 1; i <= m; i++)
	{
		scanf("%d%d%d", &nx, &ny, &cst);
		e[nx].pb(ii(ny, cst));
		e[ny].pb(ii(nx, cst));
	}

	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	b[0][1] = 1;
	for(int i = 0; i < testMax; i++)
		for(int j = 1; j < n; j++)
			if(b[i][j] && (j > 1 || i == 0))
			for(int k = 0; k < (int)e[j].size(); k++)
				if(e[j][k].first != 1)
				{
					a[i + 1][e[j][k].first] += (a[i][j] + b[i][j] * e[j][k].second);
					b[i + 1][e[j][k].first] += b[i][j];
				}

	int tot = 0, pos = 0;
	for(int i = 0; i < testMax; i++)
	{
		tot += a[i][n];
		pos += b[i][n];
	}

	double rez = (double)tot / pos;
	printf("%.3lf\n", rez);

	
	return 0;
}