Cod sursa(job #1501656)

Utilizator vladrochianVlad Rochian vladrochian Data 13 octombrie 2015 18:29:29
Problema Flux Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;

const int kMaxN = 105;
const double kEps = 1e-9;

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

int N, M;
vector<pair<int, int>> G[kMaxN];

bool use[kMaxN];

double dist[kMaxN];
double eq[kMaxN][kMaxN];

double flow;

void Dfs(int node) {
    use[node] = true;
    for (auto it : G[node])
        if (!use[it.first])
            Dfs(it.first);
}

bool Zero(double d) {
    return d > -kEps && d < kEps;
}

int main() {
    fin >> N >> M;
    while (M--) {
        int x, y, c;
        fin >> x >> y >> c;
        G[x].emplace_back(y, c);
        G[y].emplace_back(x, c);
    }

    Dfs(1);
    if (!use[N]) {
        fout << "0.000\n";
        return 0;
    }

    eq[1][1] = 1.0;
    for (int i = 2; i < N; ++i) {
        eq[i][i] = G[i].size();
        for (auto it : G[i])
            eq[i][it.first] -= 1.0;
    }
    eq[N][N] = eq[N][N + 1] = 1.0;

    for (int i = 1; i <= N; ++i)
        if (!Zero(eq[i][i]))
            for (int j = 1; j <= N; ++j) {
                if (i == j) continue;
                double v = eq[j][i] / eq[i][i];
                for (int k = 1; k <= N + 1; ++k)
                    eq[j][k] -= eq[i][k] * v;
            }


    for (int i = 1; i <= N; ++i)
        dist[i] = eq[i][N + 1] / eq[i][i];

    double k = 0;
    for (int i = 1; i <= N; ++i)
        for (auto it : G[i])
            k = max(k, (dist[it.first] - dist[i]) / it.second);

    for (auto it : G[N])
        flow += (dist[N] - dist[it.first]) / k;
    fout << setprecision(3) << fixed << flow << "\n";
    return 0;
}