Cod sursa(job #1783144)

Utilizator cella.florescuCella Florescu cella.florescu Data 18 octombrie 2016 20:10:19
Problema Tunelul groazei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 255;
const double EPS = 1e-7;

struct Node {
  int node, cost;
};

vector <Node> g[MAXN + 1];
int eq[MAXN + 2][MAXN + 2];

inline int is_null(double value) {
  return (value < EPS && value > -EPS);
}

int main()
{
    double aux;
    int n, m, x, y, c, i, j, k;
    ifstream fin("tunel.in");
    fin >> n >> m;
    for (i = 0; i < m; ++i) {
      fin >> x >> y >> c;
      g[x].push_back({y, c});
      g[y].push_back({x, c});
    }
    fin.close();
    eq[n][n] = 1.0;
    for (i = 1; i < n; ++i) {
      eq[i][i] = g[i].size();
      x = 0;
      for (auto it : g[i]) {
        eq[i][it.node] -= 1.0;
        x += it.cost;
      }
      eq[i][n + 1] = x;
    }
    for (i = 1; i <= n; ++i) {
      if (is_null(eq[i][i])) {
        for (x = i + 1; is_null(eq[i][x]); ++x) {}
        for (y = 1; y <= n + 1; ++y) {
          aux = eq[i][y]; eq[i][y] = eq[x][y]; eq[x][y] = aux;
        }
      }
      for (j = 1; j <= n; ++j)
        if (i != j)
          for (k = 1, aux = eq[j][i] / eq[i][i]; k <= n + 1; ++k)
            eq[j][k] -= eq[i][k] * aux;
    }
    ofstream fout("tunel.out");
    fout << setprecision(4) << fixed << eq[1][n + 1] / eq[1][1];
    fout.close();
    return 0;
}