Cod sursa(job #2344100)

Utilizator pas.andreiPopovici Andrei-Sorin pas.andrei Data 14 februarie 2019 19:01:01
Problema Tunelul groazei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <bits/stdc++.h>
#define NMAX 260
#define INF 0x3f3f3f3f

using namespace std;

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

const double EPS = 1e-10;
vector<pair<int, int> > G[NMAX];
double mat[NMAX][NMAX];

void printMat(int n) {
    int i,j;

    for(i=1;i<=n;++i) {
        for(j=1;j<=n+1;++j) cout << fixed << setprecision(3) << setw(7) << mat[i][j];
        cout << '\n';
    }
}

int main(){
    int i,n,m,x,y,z,j,col,lin;

    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(i=1;i<n;++i) {
        mat[i][i] = G[i].size();
        for(auto it:G[i]) {
            mat[i][it.first] -= 1.0;
            mat[i][n+1] += it.second;
        }
    }
    mat[n][n]=1;

    for(col=1;col<=n;++col) {
        for(lin=col;lin<=n;++lin) {
            if(abs(mat[lin][col]) > EPS) {
                for(i=1;i<=n+1;++i)
                    swap(mat[lin][i], mat[col][i]);

                for(i=n+1;i>=col;--i)
                    mat[col][i]/=mat[col][col];

                for(i=1;i<=n;++i) {
                    if(i == col) continue;
                    double r = mat[i][col];

                    for(j=col;j<=n+1;++j)
                        mat[i][j]-=r*mat[col][j];
                }

                break;
            }
        }
    }

    fout << fixed<<setprecision(4) << mat[1][n+1];

    return 0;
}