Cod sursa(job #1957687)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 7 aprilie 2017 18:31:58
Problema Tunelul groazei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <cstdio>
#include <vector>
#include <algorithm>

#define eps 1e-9

using namespace std;

vector <int> g[260];

int cost[260][260], p[260];
double v[260][260];

int main ()
{
    freopen ("tunel.in", "r", stdin);
    freopen ("tunel.out", "w", stdout);

    int n, m;
    scanf ("%d %d", &n, &m);

    for (int i = 1; i <= m; ++i)
    {
        int x, y, co;
        scanf ("%d %d %d", &x, &y, &co);

        cost[x][y] = cost[y][x] = co;
        g[x].push_back (y);
        g[y].push_back (x);
    }

    for (int i = 1; i <= n; ++i)
    {
        int grad = g[i].size ();
        for (auto &it : g[i])
        {
            v[i][it] += -1.0;
            v[i][n + 1] += 1.0 * cost[i][it];
        }

        v[i][i] = 1.0 * grad;
    }

    for (int i = 1; i <= n; ++i)
        v[i][n] = 0.0;

    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
            if (v[i][j] >= eps || v[i][j] <= -eps)
            {
                p[i] = j;
                break;
            }

        if (!p[i]) continue;

        for (int h = 1; h <= n; ++h)
        {
            if (h == i || -eps < v[h][p[i]] && v[h][p[i]] < eps) continue;

            double rap = v[h][p[i]] / v[i][p[i]];

            for (int j = 1; j <= n + 1; ++j)
                v[h][j] = v[h][j] - rap * v[i][j];
        }
    }

    for (int i = 1; i <= n; ++i)
        if (p[i] == 1)
        {
            printf ("%.5f\n", v[i][n + 1] / v[i][1]);
            break;
        }

    return 0;
}