Pagini recente » Cod sursa (job #1330178) | Cod sursa (job #2259748) | Cod sursa (job #2368418) | Cod sursa (job #1049545) | Cod sursa (job #3359612)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("flux.in");
ofstream fout("flux.out");
const double EPS = 1e-12;
struct Edge {
int x, y;
double c;
};
int n, m;
vector<Edge> edges;
double a[105][105], b[105], val[105];
int main() {
fin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y;
double c;
fin >> x >> y >> c;
edges.push_back({x, y, c});
if (x != 1 && x != n)
++a[x][x];
if (y != 1 && y != n)
++a[y][y];
if (x != 1 && x != n) {
if (y != 1 && y != n)
--a[x][y];
else if (y == 1)
b[x] += 1;
}
if (y != 1 && y != n) {
if (x != 1 && x != n)
--a[y][x];
else if (x == 1)
b[y] += 1;
}
}
int sz = n - 2;
for (int i = 2; i <= n - 1; ++i) {
int pos = i;
for (int j = i; j <= n - 1; ++j) {
if (abs(a[j][i]) > abs(a[pos][i]))
pos = j;
}
for (int j = i; j <= n - 1; ++j)
swap(a[i][j], a[pos][j]);
swap(b[i], b[pos]);
for (int j = i + 1; j <= n - 1; ++j) {
double coef = a[j][i] / a[i][i];
for (int k = i; k <= n - 1; ++k)
a[j][k] -= coef * a[i][k];
b[j] -= coef * b[i];
}
}
val[1] = 1;
val[n] = 0;
for (int i = n - 1; i >= 2; --i) {
val[i] = b[i];
for (int j = i + 1; j <= n - 1; ++j)
val[i] -= a[i][j] * val[j];
val[i] /= a[i][i];
}
double total = 0;
double scale = 1e100;
for (auto edge : edges) {
double flow = abs(val[edge.x] - val[edge.y]);
if (edge.x == 1)
total += val[edge.x] - val[edge.y];
if (edge.y == 1)
total += val[edge.y] - val[edge.x];
if (flow > EPS)
scale = min(scale, edge.c / flow);
}
fout << fixed << setprecision(3) << total * scale << "\n";
return 0;
}