Cod sursa(job #1783091)

Utilizator cella.florescuCella Florescu cella.florescu Data 18 octombrie 2016 19:30:01
Problema Flux Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <cstdio>
#include <fstream>
#include <iomanip>

using namespace std;

#include <vector>

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

struct Node {
  int node, cap;
};

double eq[MAXN + 2][MAXN + 2], ans[MAXN + 2];
int seen[MAXN + 2];
vector <Node> g[MAXN + 2];

void dfs(int nod, int n) {
  seen[nod] = 1;
  for (auto it : g[nod])
    if (seen[it.node] == 0)
      dfs(it.node, n);
}

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

inline double maxim(double a, double b) {
  if (a < b)
    return b;
  return a;
}

int main()
{
    double sol, aux;
    int n, m, x, y, c, k, i, j;
    ifstream fin("flux.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();
    dfs(1, n);
    ofstream fout("flux.out");
    if (seen[n] == 0)
      fout << "0.000\n";
    else {
      for (i = 2; i < n; ++i) {
        eq[i][i] = g[i].size();
        for (auto it : g[i])
          eq[i][it.node] -= 1.0;
      }
      eq[1][1] = eq[n][n] = eq[n][n + 1] = 1.0;
      for (i = 1; i <= n; ++i)
        if (is_null(eq[i][i]) == 0)
          for (j = 1; j <= n; ++j)
            if (i != j) {
              aux = eq[j][i] / eq[i][i];
              for (k = 1; k <= n + 1; ++k)
                eq[j][k] -= eq[i][k] * aux;
            }
      for (i = 1; i <= n; ++i)
        ans[i] = eq[i][n + 1] / eq[i][i];
      aux = 0.0;
      for (i = 1; i <= n; ++i)
        for (auto it : g[i])
          aux = maxim(aux, (ans[it.node] - ans[i]) / it.cap);
      sol = 0.0;
      for (auto it : g[n])
        sol += (ans[n] - ans[it.node]) / aux;
      fout << setprecision(3) << fixed << sol << '\n';
    }
    fout.close();
    return 0;
}