Cod sursa(job #2629589)

Utilizator giotoPopescu Ioan gioto Data 21 iunie 2020 17:47:28
Problema Flux Scor 88
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <bits/stdc++.h>
using namespace std;

const int DIM = 105;

struct edge {
    int x, y, c;
};

vector <edge> E;

int n, m;
double val[DIM];
double a[DIM][DIM];

void gauss() {
    int lin = 2, col = 2;
    while (lin <= n && col <= n) {
        int pos = 0;
        for (int i = lin; i <= n ; ++i)
            if (a[i][col]) {pos = lin; break ;}

        if (pos == 0) {
            ++col;
            continue ;
        }

        if (pos != lin) {
            for (int j = 1; j <= n + 1 ; ++j)
                swap(a[pos][j], a[lin][j]);
        }

        for (int j = col + 1; j <= n + 1 ; ++j)
            a[lin][j] /= a[lin][col];
        a[lin][col] = 1.0;

        for (int i = lin + 1; i <= n ; ++i) {
            for (int j = col + 1; j <= n + 1 ; ++j)
                a[i][j] -= (a[lin][j] * a[i][col]);
            a[i][col] = 0;
        }

        ++lin; ++col;
    }

    val[n] = 1;
    for (int i = n - 1; i >= 2 ; --i) {
        if (!a[i][i]) continue ;

        for (int j = i + 1; j <= n ; ++j)
            a[i][n + 1] -= (val[j] * a[i][j]);
        val[i] = a[i][n + 1] / a[i][i];
    }
}

int main()
{
    freopen("flux.in", "r", stdin);
    freopen("flux.out", "w", stdout);

    scanf("%d%d", &n, &m);
    int x, y, c;
    for (int i = 1; i <= m ; ++i) {
        scanf("%d%d%d", &x, &y, &c);
        ++a[x][y]; --a[x][x];
        --a[y][y]; ++a[y][x];
        E.push_back({x, y, c});
    }
    a[n][n + 1] = 1;
    gauss();

    double Sol = 0;
    double inm = 0.0;

    bool ok = false;
    for (auto it : E) {
        if (it.x == 1 || it.y == 1) Sol = Sol + val[it.y] + val[it.x];

        if (val[it.x] == val[it.y]) continue ;

        if (it.c == 0) {
            printf("0");
            return 0;
        }

        ok = true;
        inm = max(inm, abs(val[it.y] - val[it.x]) / (double)it.c);
    }

    printf("%0.3f", Sol / inm);

    return 0;
}