Pagini recente » Cod sursa (job #3156483) | Cod sursa (job #2831431) | Cod sursa (job #2714140) | Cod sursa (job #2280114) | Cod sursa (job #1501656)
#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;
}