Mai intai trebuie sa te autentifici.

Cod sursa(job #1159614)

Utilizator dariusdariusMarian Darius dariusdarius Data 29 martie 2014 19:08:48
Problema Tunelul groazei Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.55 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";**/
    for(int i = 1; i <= n; ++ i) {
        for(int j = 1; j <= n; ++ j) {
            if(j != i) {
                //assert(coef[i][i] > EPSILON || coef[i][i] < -EPSILON);
                double multiplier = coef[j][i] / coef[i][i];
                //fout << "Reducing " << j << " with " << i << ", multiplier = " << multiplier << "\n";
                for(int k = 1; k <= n + 1; ++ k) {
                    coef[j][k] -= coef[i][k] * multiplier;
                    //fout << coef[j][k] << (k == n + 1 ? '\n' : ' ');
                }
            }
        }
    }
    /**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";
    }**/
    for(int i = n; i >= 1; -- i) {
        for(int j = i + 1; j <= n; ++ j) {
            coef[i][n + 1] -= coef[i][j] * times[j];
        }
        times[i] = coef[i][n + 1] / coef[i][i];
        //fout << " " << times[i];
    }
    fout << times[1];
    return 0;
}