Cod sursa(job #3243199)

Utilizator not_anduAndu Scheusan not_andu Data 16 septembrie 2024 15:46:22
Problema Flux Scor 8
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <bits/stdc++.h>

using namespace std;

#define INFILE "flux.in"
#define OUTFILE "flux.out"

const int N_MAX = 100;
const double EPS = 1e-9;
const int INF = 1000;

int n, m;
bool visited[N_MAX + 5];
vector<pair<int, int> > adj[N_MAX + 5];

double flow = 0;

double dist[N_MAX + 5];
double gauss[N_MAX + 5][N_MAX + 5];

void dfs(int node){
    visited[node] = true;
    for(auto &to : adj[node]){
        if(!visited[to.first]){
            dfs(to.first);
        }
    }
}

bool isZero(double number){
    return (-N_MAX < number && number < N_MAX);
}

void solve(){

    cin >> n >> m;

    for(int i = 0; i < m; ++i){
        int node1, node2, cost; cin >> node1 >> node2 >> cost;
        adj[node1].push_back(make_pair(node2, cost));
        adj[node2].push_back(make_pair(node1, cost));
    }

    dfs(1);
    if(!visited[n]){
        cout << fixed << setprecision(3) << 0 << '\n';
    }
    else {

        gauss[1][1] = 1.0;
        for(int i = 2; i < n; ++i){
            if(!isZero(gauss[1][1])){
                for(int j = 1; j <= n; ++j){
                    if(i == j) continue;
                    double k = gauss[j][i] / gauss[i][i];
                    for(int ind = 1; ind <= n + 1; ++i){
                        gauss[j][ind] -= gauss[i][ind] * k;
                    }
                }
            }
        }

        for(int i = 1; i <= n; ++i){
            dist[i] = gauss[i][n + 1] / gauss[i][i];
        }

        double k = 0;
        for(int i = 1; i <= n; ++i){
            for(auto &to : adj[i]){
                k = max(k, (dist[to.first] - dist[i]) / to.second);
            }
        }

        for(auto &to : adj[n]){
            flow += (dist[n] - dist[to.first]) / k;
        }

        cout << setprecision(3) << fixed << flow << '\n';

    }

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);
    solve();
    return 0;
}