Cod sursa(job #2629596)

Utilizator giotoPopescu Ioan gioto Data 21 iunie 2020 18:04:58
Problema Flux Scor 88
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.15 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 = i; break ;}

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

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

        for (int j = col + 1; j <= n ; ++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 ; ++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 > 1 ; --i) {
        for (int j = 2; j <= n ; ++j) {
            if (a[i][j]){
                //Calculam pe necunoscuta j = rezultatul ecuatiei i - necunoscutele j+1...M inmultite cu coeficientii lor din aceasta ecuatie.
                val[j] = a[i][n + 1];
                for (int k = j + 1; k <= n ; ++k)
                    val[j] -= val[k] * a[i][k];

                break ;
            }
        }
    }
}

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});
    }
    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]) / it.c);
    }

    if (!ok) printf("0");
    else printf("%0.3f", Sol / inm);

    return 0;
}