Cod sursa(job #2347626)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 18 februarie 2019 22:23:37
Problema Tunelul groazei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.35 kb
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second
#define DEBUG(x) cerr << (#x) << ": " << (x) << '\n'

using namespace std;

typedef pair <int, int> pii;
typedef vector <int> vi;
typedef long long ll;
typedef unsigned long long ull;

template <class T> void uin (T &a, T b) {a = min (a, b);}
template <class T> void uax (T &a, T b) {a = max (a, b);}

ifstream f ("tunel.in");
ofstream g ("tunel.out");

const int NMAX = 300;
const double EPS = 1e-3;

int n, m;
double equation[NMAX][NMAX];
double ans[NMAX];

inline int sign (double x) {
  if (fabs (x) <= EPS) return 0;
  return (x < 0) ? -1 : 1;
}

void swapRows (int r1, int r2) {
  for (int i = 1; i <= m + 1; ++i) {
    swap (equation[r1][i], equation[r2][i]);
  }
}

void gauss() {
  --n;
  m = n + 1;
  int i = 1, j = 1;
  while (i <= n && j <= m) {
    int k = i - 1;
    while ((++k <= n) && sign (equation[k][j]) == 0);
    if (k > n) {
      ++j;
      continue;
    }

    if (k != i) swapRows (i, k);

    for (int col = j + 1; col <= m + 1; ++col) {
      equation[i][col] /= equation[i][j];
    }
    equation[i][j] = 1.0;

    for (int row = i + 1; row <= n; ++row) {
      for (int col = j + 1; col <= m + 1; ++col) {
        equation[row][col] -= equation[row][j] * equation[i][col];
      }
      equation[row][j] = 0.0;
    }

    ++i;
    ++j;
  }


  for (int i = n; i >= 1; --i) {
    for (int j = 1; j <= m + 1; ++j) {
      if (sign (equation[i][j]) == 0) continue;

      ans[j] = equation[i][m + 1];
      for (int col = j + 1; col <= m; ++col) {
        ans[j] -= equation[i][col] * ans[col];
      }
      break;
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen (".in", "r", stdin);
#endif

  g.precision (3);
  g << fixed;

  f >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int u, v, cost;
    f >> u >> v >> cost;
    equation[u][u] += 1.0;
    equation[v][v] += 1.0;

    equation[u][v] -= 1.0;
    equation[v][u] -= 1.0;

    equation[u][n + 1] += 1.0 * cost;
    equation[v][n + 1] += 1.0 * cost;
  }

  gauss();

  g << ans[1] << '\n';

  f.close();
  g.close();

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}