Cod sursa(job #1159634)

Utilizator dariusdariusMarian Darius dariusdarius Data 29 martie 2014 19:20:17
Problema Tunelul groazei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.62 kb
#include <cassert>
#include <cmath>

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
const int MAX_N = 260;
const double EPSILON = 1.e-5;

vector< pair<int, double> > graph[MAX_N];
double coef[MAX_N][MAX_N];
double times[MAX_N];

inline void swap_line(int n, int x, int y) {
    for(int j = 1; j <= n + 1; ++ j) {
        swap(coef[x][j], coef[y][j]);
    }
}

int main() {
    ifstream fin("tunel.in");
    ofstream fout("tunel.out");
    int n, m;
    fin >> n >> m;
    for(int i = 1; i <= m; ++ i) {
        int a, b; double c;
        fin >> a >> b >> c;
        graph[a].push_back(make_pair(b, c));
        graph[b].push_back(make_pair(a, c));
    }
    for(int i = 1; i < n; ++ i) {
        for(int j = 1; j <= n; ++ j) {
            coef[i][j] = 0.0;
        }
        coef[i][i] = -1.0;
        double degree = 1.0 / static_cast<double>(graph[i].size());
        for(vector< pair<int, double> > :: iterator it = graph[i].begin(); it != graph[i].end(); ++ it) {
            coef[i][it->first] += degree;
            coef[i][n + 1] -= degree * it->second;
        }
    }
    coef[n][n] = 1.0;
    /**for(int i = 1; i <= n; ++ i) {
        for(int j = 1; j <= n; ++ j) {
            if(j > 1) {
                fout << "+ ";
            }
            fout << fixed << setw(3) << setprecision(2) << coef[i][j] << "*t(" << j << ") ";
        }
        fout << "= " << coef[i][n + 1] << "\n";
    }
    fout << "\n";**/
    int p = 1;
    for(int i = 1; i <= n; ++ i) {
        if(fabs(coef[p][i]) < EPSILON) {
            for(int j = p + 1; j <= n; ++ j) {
                if(fabs(coef[j][i]) >= EPSILON) {
                    swap_line(n, p, j);
                    break;
                }
            }
            if(fabs(coef[p][i]) < EPSILON) {
                continue;
            }
        }
        for(int j = 1; j <= n; ++ j) {
            if(j != p) {
                double multiplier = coef[j][i] / coef[i][i];
                for(int k = 1; k <= n + 1; ++ k) {
                    coef[j][k] -= coef[i][k] * multiplier;
                }
            }
        }
        ++ p;
    }
    /**for(int i = 1; i <= n; ++ i) {
        for(int j = 1; j <= n; ++ j) {
            if(j > 1) {
                fout << "+ ";
            }
            fout << fixed << setw(3) << setprecision(2) << coef[i][j] << "*t(" << j << ") ";
        }
        fout << "= " << coef[i][n + 1] << "\n";
    }**/
    p = 1;
    for(int i = 1; i <= n; ++ i) {
        if(coef[p][i]) {
            times[i] = coef[p][n + 1] / coef[p][i];
            ++ p;
        }
    }
    fout << fixed << setprecision(4) << times[1];
    return 0;
}