Cod sursa(job #1501646)

Utilizator vladrochianVlad Rochian vladrochian Data 13 octombrie 2015 18:22:43
Problema Flux Scor 72
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>
#include <vector>
using namespace std;

const int kMaxN = 105;
const double kEps = 1e-9;

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

int N, M;
vector<pair<int, int>> G[kMaxN];

double dist[kMaxN];
double eq[kMaxN][kMaxN];

double flow;

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

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

  eq[1][1] = 1.0;
  for (int i = 2; i < N; ++i) {
    eq[i][i] = G[i].size();
    for (auto it : G[i])
      eq[i][it.first] -= 1.0;
  }
  eq[N][N] = eq[N][N + 1] = 1.0;

  for (int i = 1; i <= N; ++i) {
    if (Zero(eq[i][i]))
        continue;
    for (int j = 1; j <= N; ++j) {
      if (i == j) continue;
      double v = eq[j][i] / eq[i][i];
      for (int k = 1; k <= N + 1; ++k)
        eq[j][k] -= eq[i][k] * v;
    }
  }

  for (int i = 1; i <= N; ++i)
    dist[i] = eq[i][N + 1] / eq[i][i];

  double k = 0;
  for (int i = 1; i <= N; ++i)
    for (auto it : G[i])
      k = max(k, (dist[it.first] - dist[i]) / it.second);

  for (auto it : G[N])
    flow += (dist[N] - dist[it.first]) / k;
  fout << flow << "\n";
  return 0;
}