Cod sursa(job #2719247)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 9 martie 2021 18:36:42
Problema Flux Scor 68
Compilator cpp-64 Status done
Runda Lista lui wefgef Marime 1.71 kb
#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

const long double EPS = 1e-6;
const int MAGIC = 1000;

int Sgn(long double x) {
  if (fabs(x) <= EPS) return 0;
  return x < 0 ? -1 : +1;
}

vector<long double> Gauss(vector<vector<long double>> s) {
  int n = s.size(), m = s[0].size();
  int i = 0, j = 0;
  while (i < n && j < m - 1) {
    int k = i; while(k < n && Sgn(s[k][j]) == 0) ++k;
    if (k == n) {
      ++j;
      continue;
    }
    if (i != k) swap(s[i], s[k]);

    for (k = 0; k < m; ++k) if (k != j) s[i][k] /= s[i][j];
    s[i][j] = 1;

    for (k = 0; k < n; ++k) if (i != k) {
      auto coef = s[k][j];
      if (Sgn(coef) != 0) {
        for (int c = 0; c < m; ++c)
          s[k][c] -= coef * s[i][c];
        s[k][j] = 0;
      }
    }
    ++i, ++j;
  }

  vector<long double> x(n);
  for (i = 0; i < n; ++i) {
    x[i] = s[i][m - 1];
  }
  return x;
}

int main() {
  ifstream cin("flux.in");
  ofstream cout("flux.out");

  int n, m; cin >> n >> m;
  vector<tuple<int, int, int>> edges(m);
  vector<vector<long double>> s(n, vector<long double>(n + 1));
  for (int i = 0; i < m; ++i) {
    int a, b, c; cin >> a >> b >> c; --a, --b;
    if (a > b) swap(a, b);
    edges[i] = {a, b, c};
    if (a != 0) {
      s[a][b] += 1; s[a][a] -= 1;
    }
    s[b][a] += 1; s[b][b] -= 1;
  }
  s[0][0] = 1; s[0][n] = 0; // dist[0] = 0
  s[n - 1][n] = -MAGIC;

  auto dist = Gauss(move(s));

  long double ans = numeric_limits<long double>::max();
  for (auto &e : edges) {
    int a, b, c; tie(a, b, c) = e;
    long double flow = abs(dist[a] - dist[b]);
    if (Sgn(flow) != 0) ans = min(ans, c / flow);
  }
  cout << fixed << setprecision(10) << ans * MAGIC << endl;
}