Cod sursa(job #1692519)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 21 aprilie 2016 00:24:53
Problema Tunelul groazei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <iomanip>

using namespace std;

ifstream fin("tunel.in");
ofstream fout("tunel.out");

const int dim = 260;
const double eps = 1e-7;

int n, m;

int edgeCount[dim][dim], deg[dim];
vector<int> costs[dim];

double a[dim][dim], res[dim];

int main() {

	fin >> n >> m;

	for (int i = 1; i <= m; ++i) {

		int x, y, c;
		fin >> x >> y >> c;

		edgeCount[x][y]++;
		edgeCount[y][x]++;
		deg[x]++, deg[y]++;
		costs[x].push_back(c);
		costs[y].push_back(c);

	}

	for (int i = 1; i <= n; ++i) {

		for (int j = 1; j < n; ++j)
			a[i][j] = -1.0 * edgeCount[i][j] / (1.0 * deg[i]);

		if (i != n)
			a[i][i] = 1.0;

		for (unsigned int j = 0; j < costs[i].size(); ++j)
			a[i][n + 1] += 1.0 * costs[i][j] / (1.0 * deg[i]);

	}

	for (int i = 1, j = 1; i <= n && j <= n; ++i, ++j) {

		bool ok = false;
		for (int k = i; k <= n && !ok; ++k) {

			if (-eps < a[k][j] && a[k][j] < eps)
				continue;

			for (int l = 1; l <= n + 1; ++l)
				swap(a[i][l], a[k][l]);

			ok = true;

		}

		if (!ok) {
			--i;
			continue;
		}

		for (int l = j + 1; l <= n + 1; ++l)
			a[i][l] /= a[i][j];
		a[i][j] = 1.0;

		for (int k = i + 1; k <= n; ++k) {

			for (int l = j + 1; l <= n + 1; ++l)
				a[k][l] -= a[k][j] * a[i][l];
			a[k][j] = 0.0;

		}

	}

	for (int i = n; i; --i) {
		for (int j = 1; j <= n; ++j) {

			if (-eps < a[i][j] && a[i][j] < eps)
				continue;

			res[j] = a[i][n + 1];
			for (int l = j + 1; l <= n; ++l)
				res[j] -= res[l] * a[i][l];
			res[j] /= a[i][j];

		}
	}

	fout << setprecision(4) << fixed << res[1] << '\n';

	return 0;

}

//Trust me, I'm the Doctor!