Cod sursa(job #1522354)

Utilizator bogdan10bosBogdan Sitaru bogdan10bos Data 11 noiembrie 2015 16:46:12
Problema Tunelul groazei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <bits/stdc++.h>

#define INF (1 << 30)
#define LLINF (1LL << 62)
#define mod 666013
#define pb push_back
#define mp make_pair
#define eps 0.00000001

using namespace std;

typedef pair<int, int> pereche;

int n, m, i, j, k, x, y, z;
int st[270];
double coef, sol;
double a[270][270];

vector <pereche> v[270];
vector <pereche> :: iterator it;

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

    scanf("%d%d", &n, &m);
    while(m--)
    {
        scanf("%d%d%d", &x, &y, &z);
        v[x].pb(mp(y, z));
        v[y].pb(mp(x, z));
    }

    for(i = 1; i < n; i++)
        if(v[i].size())
        {
            coef = (double)1 / (double)v[i].size();
            for(it = v[i].begin(); it != v[i].end(); it++)
            {
                a[i][(*it).first] += coef;
                a[i][n + 1] -= coef * (*it).second;
            }
            a[i][i] = -1;
        }

    for(i = 1; i <= n; i++)
        a[i][n] = 0;

    for(i = 1; i <= n; i++)
    {
        for(st[i] = 1; st[i] <= n + 1 && fabs(a[i][st[i]]) < eps; st[i]++);

        if(st[i] == n + 1)
        {
            printf("-1");
            return 0;
        }

        if(st[i] > n + 1)
        {
            st[i] = 0;
            continue;
        }

        for(j = 1; j <= n; j++)
        {
            if(i == j || fabs(a[j][st[i]]) < eps)
                continue;

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

    sol = a[1][n + 1] / a[1][1];

    printf("%.3f", sol);

    return 0;
}