Cod sursa(job #1193844)

Utilizator smaraldaSmaranda Dinu smaralda Data 1 iunie 2014 23:59:29
Problema Tunelul groazei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;

const int NMAX = 255, MMAX = 1e5;
const double eps = 0.001;

int n, x[MMAX + 5], y[MMAX + 5], deg[NMAX + 5];
double coef[NMAX + 5][NMAX + 5], c[MMAX + 5], sum[NMAX + 5];

void swapLines (int a, int b) {
    if(a == b)
        return;

    int j;
    for(j = 1; j <= n + 1; ++ j)
        swap(coef[a][j], coef[b][j]);
}

void gauss() {
    int i, j, k;
    double alpha;

    for(j = 1; j <= n; ++ j) {
        for(i = j; i <= n; ++ i)
            if(coef[i][j]) {
                swapLines(j, i);
                break;
            }

        for(i = 1; i <= n; ++ i)
            if(i != j) {
                alpha = coef[i][j] / coef[j][j];

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

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

    scanf("%d%d", &n, &m);
    for(i = 1; i <= m; ++ i) {
        scanf("%d%d%lf", &x[i], &y[i], &c[i]);
        ++ deg[x[i]];
        ++ deg[y[i]];
        sum[x[i]] += c[i];
        sum[y[i]] += c[i];
    }

    for(i = 1; i < n; ++ i)
        coef[i][i] = deg[i],
        coef[i][n + 1] = (double)sum[i];

    for(i = 1; i <= m; ++ i)
        coef[x[i]][y[i]] = -1,
        coef[y[i]][x[i]] = -1;

    memset(coef[n], 0, sizeof(coef[n]));
    coef[n][n] = 1;

    gauss();

    printf("%lf\n", coef[1][n + 1] / coef[1][1]);
    return 0;
}