Cod sursa(job #1479192)

Utilizator vladrochianVlad Rochian vladrochian Data 30 august 2015 18:38:58
Problema Tunelul groazei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;

const int kMaxN = 255;
const double kEps = 1e-8;

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

int N;
vector<pair<int, int>> al[kMaxN + 5];

double a[kMaxN + 5][kMaxN + 5];

bool Zero(double d) {
  return (d > -kEps) && (d < kEps);
}

void Read() {
  int M;
  fin >> N >> M;
  while (M--) {
    int x, y, c;
    fin >> x >> y >> c;
    al[x].emplace_back(y, c);
    al[y].emplace_back(x, c);
  }
}

void Build() {
  for (int i = 1; i < N; ++i) {
    int s = 0;
    for (auto it : al[i]) {
      a[i][it.first] = 1.0;
      s += it.second;
    }
    a[i][i] = -int(al[i].size());
    a[i][N + 1] = -s;
  }
  a[N][N] = 1.0;
}

void Gauss() {
  for (int k = 1; k <= N; ++k) {
    if (Zero(a[k][k])) {
      int i = k + 1;
      while (Zero(a[k][i]))
        ++i;
      swap(a[k], a[i]);
    }
    for (int i = 1; i <= N; ++i) {
      if (i == k) continue;
      double fact = a[i][k] / a[k][k];
      for (int j = 1; j <= N + 1; ++j)
        a[i][j] -= fact * a[k][j];
    }
  }
}

int main() {
  Read();
  Build();
  Gauss();
  fout << setprecision(4) << fixed << (a[1][N + 1] / a[1][1]) << "\n";
  return 0;
}