Cod sursa(job #1513953)

Utilizator ZenusTudor Costin Razvan Zenus Data 30 octombrie 2015 12:29:11
Problema Tunelul groazei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <bits/stdc++.h>

using namespace std;

const int nmax = 255 + 10;

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

vector < pair < int , int > > g[nmax];
int x , y , z , n , m , i , j , k , p;
double c , a[nmax][nmax];

bool zero(double x)
{
    double eps = 1e-8;

    if (-eps < x && x < eps) return true;
    return false;
}

int main()
{

fin >> n >> m;

for (i = 1 ; i <= m ; ++i)
{
    fin >> x >> y >> z;
    g[x].push_back({y , z});
    g[y].push_back({x , z});
}

for (x = 1 ; x < n ; ++x)
{
    a[x][x] = g[x].size();

    for (j = 0 ; j < g[x].size() ; ++j)
    {
        y = g[x][j].first;
        z = g[x][j].second;

        a[x][y] -= 1;
        a[x][n + 1] += z;
    }
}

a[n][n] = 1.0;

for (i = 1 ; i <= n ; ++i)
{
    p = 0;

    for (j = 1 ; j <= n + 1 ; ++j)
    {
        if (zero(a[i][j])) continue;
        p = j;
        break;
    }

    if (p == 0) continue;

    for (j = 1 ; j <= n ; ++j)
    if (i - j)
    {
        if (zero(a[j][p])) continue;

        c = a[j][p] / a[i][p];
        for (k = 1 ; k <= n + 1 ; ++k)
        a[j][k] -= c * a[i][k];
    }
}

fout << fixed << setprecision(10) << a[1][n + 1] / a[1][1] << '\n';

return 0;
}