Cod sursa(job #2922063)

Utilizator lolismekAlex Jerpelea lolismek Data 3 septembrie 2022 15:58:02
Problema Flux Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.99 kb
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <cmath>

using namespace std;

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

struct edge{
	int a, b, c;
};

const int N = 100;
const double eps = 1e-8, inf = 1e9;
double coef[N + 2][N + 2], sol[N + 1]; 

bool notNull(double x){
	return (x < -eps || x > eps);
}
 
void Gauss(double coef[N + 2][N + 2], double sol[N + 1], int n, int m){
	int i = 1, j = 1;
	while(i <= n && j <= m){
		int k = i;
		while(k <= n){
			if(notNull(coef[k][j]))
				break;
			k++;
		}
 
		if(k == n + 1){
			j++;
			continue;
		}
 
		if(k != i)
			for(int jj = 1; jj <= m + 1; jj++)
				swap(coef[k][jj], coef[i][jj]);
		
		for(int jj = m + 1; jj >= j; jj--)
			coef[i][jj] /= coef[i][j];
 
		for(int ind = i + 1; ind <= n; ind++)
			for(int jj = m + 1; jj >= j; jj--)
				coef[ind][jj] -= coef[ind][j] * coef[i][jj];
 
		i++, j++;
	}
 
	for(int i = n; i >= 1; i--)
		for(int j = 1; j <= m + 1; j++)
			if(notNull(coef[i][j])){
				if(j == m + 1)
					exit(0); 
 
				sol[j] = coef[i][m + 1];
				for(int jj = j + 1; jj <= m; jj++)
					sol[j] -= sol[jj] * coef[i][jj];
 
				break;
			}
}

int main(){

	int n, m;
	fin >> n >> m;

	vector <edge> E;
	for(int i = 1; i <= m; i++){
		edge e;
		fin >> e.a >> e.b >> e.c;

		E.push_back(e);

		if(e.c != 0){
			if(min(e.a, e.b) != 1){
				coef[min(e.a, e.b)][min(e.a, e.b)]--;
				coef[min(e.a, e.b)][max(e.a, e.b)]++;
			}
			if(max(e.a, e.b) != n){
				coef[max(e.a, e.b)][max(e.a, e.b)]--;
				coef[max(e.a, e.b)][min(e.a, e.b)]++;
			}
		}
	}	

	coef[1][1] = 1; // d[1] = 0
	coef[n][n] = 1, coef[n][n + 1] = 1; // d[n] = 1

	Gauss(coef, sol, n, n);

	double ratio = inf;
	for(auto e : E)
		ratio = min(ratio, e.c / abs(sol[e.a] - sol[e.b]));
	
	double ans = 0;
	for(auto e : E)
		ans += ratio * abs(sol[e.a] - sol[e.b]) * (max(e.a, e.b) == n);

	fout << fixed << setprecision(3);
	fout << ans << '\n';

	return 0;
}