Cod sursa(job #3140091)

Utilizator euyoTukanul euyo Data 3 iulie 2023 19:08:58
Problema Flux Scor 92
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 105;
const double EPS = 1e-8;

double t[DIM][DIM];
double res[DIM];

void Gauss( int n, int m ) {
   for ( int l = 1, c = 1; l <= n && c <= m; ++c ) {
	int nxt = l;
	while ( nxt <= n && abs( t[nxt][c] ) < EPS ) {
	  ++nxt;
	}
	if ( nxt > n ) continue;
    for ( int i = 1; i <= m + 1; ++i ) {
	  swap( t[nxt][i], t[l][i] );
	}
    
	for ( int i = c + 1; i <= m + 1; ++i ) {
	  t[l][i] /= t[l][c];
	}
	t[l][c] = 1;
    for ( int nl = l + 1; nl <= n; ++nl ) {
	  for ( int nc = c + 1; nc <= m + 1; ++nc ) {
	    t[nl][nc] -= t[nl][c] * t[l][nc]; 	
	  }
	  t[nl][c] = 0;
	}
    ++l;   
  }
  for ( int i = n; i > 0; --i ) {
    int j = 1; 
	while ( j <= m && abs( t[i][j] ) < EPS ) {
      ++j;
	}
    res[j] = t[i][m + 1];
    for ( int nj = j + 1; nj <= m; ++nj ) {
	  res[j] -= res[nj] * t[i][nj];
	}
  }
}

vector<pair<int, int>> G[DIM];

int main() {
  int n, m, u, v, c;

  fin >> n >> m;
  while ( m-- ) {
	fin >> u >> v >> c;
	G[u].push_back({v, c});
	G[v].push_back({u, c});
  }
  for ( u = 2; u <= n; ++u ) {
	for ( auto qq : G[u] ) {
	  --t[u - 1][qq.first - 1];
	}
	t[u - 1][u - 1] += G[u].size();
	if ( u == n ) {
	  t[u - 1][n] = 1;
	}
  }
  Gauss(n - 1, n - 1);
  double sol = 1e9;
  for ( u = 1; u <= n; ++u ) {
	assert(res[u - 1] >= 0);
	for ( auto qq : G[u] ) {
      if ( abs(res[u - 1] - res[qq.first - 1]) < EPS ) continue;
	  sol = min(sol, abs((double)qq.second / (res[u - 1] - res[qq.first - 1])));
	}
  }
  fout << setprecision(8) << fixed << sol;
  fin.close();
  fout.close();
  return 0;
}