Pagini recente » Cod sursa (job #1887793) | Cod sursa (job #3122335) | Cod sursa (job #103837) | Cod sursa (job #1097773) | Cod sursa (job #1501649)
#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];
double dist[kMaxN];
double eq[kMaxN][kMaxN];
double flow;
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);
}
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]))
continue;
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;
}