Cod sursa(job #3185417)

Utilizator verde.cristian2005Verde Flaviu-Cristian verde.cristian2005 Data 18 decembrie 2023 20:30:45
Problema Tunelul groazei Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <bits/stdc++.h>
using namespace std;

#ifndef HOME
    ifstream in("tunel.in");
    ofstream out("tunel.out");
    #define cin in
    #define cout out
#endif

long double coef[256][257];
const long double eps = 1e-8;

int main()
{
#ifdef HOME
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#endif
    ///dp[i] = timpul mediu de a ajunge din i in n
    ///dp[i] = suma(dp[j] + cost[i][j] | i are muchie cu j) / grad[i] | *grad[i]
    ///dp[i] * grad[i] = suma(dp[j] | i are muchie cu j) + sumcost[i]
    ///grad[i] * dp[i] - suma(dp[j] | i are muchie cu j) = sumcost[i]
    int n, m, a, b, c;
    cin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        cin >> a >> b >> c;
        ///pentru a
        coef[a][a]++;
        coef[a][b]--;
        coef[a][n + 1] += c;
        ///pentru b
        coef[b][b]++;
        coef[b][a]--;
        coef[b][n + 1] += c;
    }
    for(int j = 1; j <= n - 1; j++)
        coef[n][j] = 0;
    coef[n][n] = 1;
    coef[n][n + 1] = 0;
    int lastEq = 1;
    for(int j = 1; j <= n; j++)
    {
        for(int i = lastEq; i <= m; i++)
            if(coef[i][j] >= eps || coef[i][j] <= -eps)
            {
                for(int k = 1; k <= n + 1; k++)
                    swap(coef[lastEq][k], coef[i][k]);
                break;
            }
        if(coef[j][j] >= eps || coef[j][j] <= -eps)
        {
            long double x = coef[lastEq][j];
            for(int k = 1; k <= n + 1; k++)
                coef[lastEq][k] /= x;
            for(int i = 1; i <= m; i++)
                if(i != j)
                {
                    long double x = coef[i][j];
                    for(int k = 1; k <= n + 1; k++)
                        coef[i][k] -= coef[lastEq][k] * x;
                    /// bk -= ak * bj
                }
            lastEq++;
        }
    }
    cout << fixed << setprecision(6) << coef[1][n + 1];
    return 0;
}