Cod sursa(job #3202634)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 12 februarie 2024 01:18:27
Problema Tunelul groazei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <bits/stdc++.h>
#ifdef BLAT
    #include "debug/debug.hpp"
#else
    #define debug(x...)
#endif

using namespace std;

ifstream in ("tunel.in");
ofstream out ("tunel.out");

const double EPS = 0.001;

int main() {
    int n, m;
    in >> n >> m;
    vector<vector<double>> a(n, vector<double>(n + 1));

    for (int i = 0; i < m; i++) {
        int x, y, w;
        in >> x >> y >> w;
        a[x - 1][y - 1]--; a[y - 1][x - 1]--;
        a[x - 1][x - 1]++; a[y - 1][y - 1]++;
        a[x - 1][n] += w;
        a[y - 1][n] += w;
    }

    for (int j = 0; j <= n; j++)
        a[n - 1][j] = 0;
    a[n - 1][n - 1] = 1;

    for (int row = 0, col = 0; row < n; row++, col++) {
        for (int pivot_row = row; pivot_row < n; pivot_row++)
            if (fabs(a[pivot_row][col]) > EPS) {
                swap(a[row], a[pivot_row]);
                break;
            }

        if (fabs(a[row][col]) <= EPS)
            continue;

        for (int _col = col + 1; _col <= n; _col++)
            a[row][_col] = a[row][_col] / a[row][col];
        a[row][col] = 1;

        for (int _row = 0; _row < n; _row++) {
            if (row == _row)
                continue;

            for (int _col = 0; _col <= n; _col++)
                if (_col != col)
                    a[_row][_col] -= a[_row][col] * a[row][_col];
            a[_row][col] = 0;
        }
    }

    out << fixed << setprecision(5) << a[0][n] << '\n';
    in.close();
    out.close();
    return 0;
}