Cod sursa(job #3314176)

Utilizator Cristian_NegoitaCristian Negoita Cristian_Negoita Data 8 octombrie 2025 19:52:07
Problema Tunelul groazei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("tunel.in");
ofstream fout("tunel.out");
const int NMAX = 256;
double A[NMAX][NMAX + 1];
const double EPS = 1e-6;

int main()
{
    int n, m;
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int u, v, cost;
        fin >> u >> v >> cost;
        A[u][u]++;
        A[u][v]--;
        A[u][n + 1] += cost;
        A[v][v]++;
        A[v][u]--;
        A[v][n + 1] += cost;
    }
    for(int j = 1; j <= n - 1; j++)
        A[n][j] = 0;
    A[n][n] = 1;
    A[n][n + 1] = 0;
    int last = 1;
    m = n;
    for(int j = 1; j <= n; j++)
    {
        for(int i = last; i <= m; i++)
        {
            if(abs(A[i][j]) >= EPS)
            {
                for(int k = 1; k <= n + 1; k++)
                    swap(A[last][k], A[i][k]);
                break;
            }
        }
        if(abs(A[j][j]) >= EPS)
        {
            double x = A[last][j];
            for(int k = 1; k <= n + 1; k++)
                A[last][k] /= x;
            for(int i = 1; i <= m; i++)
            {
                if(i != j)
                {
                    double x = A[i][j];
                    for(int k = 1; k <= n + 1; k++)
                        A[i][k] -= A[last][k] * x;
                }
            }
            last++;
        }
    }
    fout << fixed << setprecision(6) << A[1][n + 1];

    fin.close();
    fout.close();
    return 0;
}