Cod sursa(job #2279324)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 9 noiembrie 2018 13:27:04
Problema Flux Scor 96
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.29 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 100 + 5;
const double eps = 1e-7;

int n;
double sol[nmax + 1];
double a[nmax + 1][nmax + 1];

struct str {
    int x, y, c;
};
vector<str> v;

bool gauss () {
    int lin = 1;
    for (int i = 1; i <= n; ++ i) {
        int ind = 0;
        while (i <= n) {
            ind = lin;
            while (ind <= n && fabs(a[ind][i]) <= eps)
                ++ ind;

            if (ind <= n && fabs(a[ind][i]) > eps)
                break;
            ++ i;
        }

        if (i <= n) {
            for (int j = 1; j <= n + 1; ++ j)
                swap(a[ind][j], a[lin][j]);

            for (int j = lin + 1; j <= n; ++ j) {
                if (fabs(a[j][i]) <= eps) continue;

                double r = a[j][i] / a[lin][i];
                for (int k = i; k <= n + 1; ++ k) {
                    a[j][k] -= r * a[lin][k];
                }
            }
            ++ lin;
        }
    }

    for (int i = n; i > 0; -- i) {
        int j = 0;
        while (j <= n && fabs(a[i][j]) <= eps)
            ++ j;

        if (j == n + 1) {
            if (fabs(a[i][n + 1]) > eps)
                return 0;
            continue;
        }

        for (int k = j + 1; k <= n; ++ k)
            a[i][n + 1] -= sol[k] * a[i][k];

        sol[j] = a[i][n + 1] / a[i][j];
    }
    return 1;
}

int main () {
    int m;
    fin >> n >> m;

    for (int i = 1; i <= m; ++ i) {
        str shp;
        fin >> shp.x >> shp.y >> shp.c;
        v.push_back(shp);

        ++ a[shp.x][shp.x];
        -- a[shp.x][shp.y];

        ++ a[shp.y][shp.y];
        -- a[shp.y][shp.x];
    }

    for (int i = 1; i <= n; ++ i) {
        a[1][i] = 0;
        a[n][i] = 0;
    }
    a[1][1] = 1;
    a[n][n] = a[n][n + 1] = 1;

    if (gauss() == 0) {
        fout << "0\n";
        return 0;
    }

    double rmin = INFINITY;
    for (auto i : v) {
        if (fabs(sol[i.y] - sol[i.x]) > eps) {
            rmin = min(rmin, (double)i.c / fabs(sol[i.y] - sol[i.x]));
        }
    }

    double ans = 0;
    for (auto i : v) {
        if (i.x == n || i.y == n)
            ans += rmin * fabs(sol[i.x] - sol[i.y]);
    }

    fout << setprecision(6) << fixed;
    fout << ans << "\n";

    fin.close();
    fout.close();
    return 0;
}