Cod sursa(job #2920800)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 25 august 2022 21:32:34
Problema Tunelul groazei Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <bits/stdc++.h>

using namespace std;

typedef pair <int, double> PID;
constexpr int NMAX = 256;
constexpr double eps = 1e-8;

ifstream f ("tunel.in");
ofstream g ("tunel.out");
int N, M;
vector <PID> G[NMAX];

double modul (double x) {
    if (x < 0) x = -x;

    return x;
}

void Read () {
    f >> N >> M;

    for (int i = 1; i <= M; ++ i ) {
        int x, y;
        double C;

        f >> x >> y >> C;

        G[x].push_back({y, C});
        G[y].push_back({x, C});
    }
}

double Exp[NMAX];
double A[NMAX][NMAX];
double ans[NMAX];

void PrecomputeEquations () {
    for (int i = 1; i < N; ++ i ) {
        for (auto it : G[i]) {
            int to = it.first;
            double cost = it.second;

            A[i][i] ++;
            if (to != N) A[i][to] --;
            A[i][N] += cost;
        }
    }
}

int P[NMAX];

void Solve () {
    for (int i = 1; i < N; ++ i ) {
        int j = 0;
        P[i] = 0;

        for (j = 1; j <= N; ++ j )
            if (modul(A[i][j]) > eps) break;

        if (j == N) {
            cerr << "Wtf ?" << '\n';
            exit(0);
        }

        if (j == N+1) continue;

        P[i] = j;

        for (int j = 1; j < N; ++ j ) {
            if (i != j && modul(A[j][P[i]]) > eps) {
                double factor = A[j][P[i]] / A[i][P[i]];

                for (int k = 1; k <= N; ++ k )
                    A[j][k] -= factor * A[i][k];
            }
        }
    }

    for (int i = 1; i <= N; ++ i )
        if (P[i] != 0)
            ans[P[i]] = A[i][N] / A[i][P[i]];

    g << setprecision(4) << fixed << ans[1] << '\n';
}

int main () {
    Read();
    PrecomputeEquations();
    Solve();

    return 0;
}