Pagini recente » Cod sursa (job #2157769) | Cod sursa (job #2023968) | Cod sursa (job #1742574) | Cod sursa (job #1750696) | Cod sursa (job #2279321)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("flux.in"); ofstream fout ("flux.out");
const int nmax = 100 + 5;
const double eps = 1e-7;
int n;
double sol[nmax + 1];
double a[nmax + 1][nmax + 1];
struct str {
int x, y, c;
};
vector<str> v;
void gauss () {
int lin = 1;
for (int i = 1; i <= n; ++ i) {
int ind = 0;
while (i <= n) {
ind = lin;
while (ind <= n && fabs(a[ind][i]) <= eps)
++ ind;
if (ind <= n && fabs(a[ind][i]) > eps)
break;
++ i;
}
if (i <= n) {
for (int j = 1; j <= n + 1; ++ j)
swap(a[ind][j], a[lin][j]);
for (int j = lin + 1; j <= n; ++ j) {
if (fabs(a[j][i]) <= eps) continue;
double r = a[j][i] / a[lin][i];
for (int k = i; k <= n + 1; ++ k) {
a[j][k] -= r * a[lin][k];
}
}
++ lin;
}
}
for (int i = n; i > 0; -- i) {
int j = 0;
while (j <= n && fabs(a[i][j]) <= eps)
++ j;
if (j == n + 1)
continue;
for (int k = j + 1; k <= n; ++ k)
a[i][n + 1] -= sol[k] * a[i][k];
sol[j] = a[i][n + 1] / a[i][j];
}
}
int main () {
int m;
fin >> n >> m;
for (int i = 1; i <= m; ++ i) {
str shp;
fin >> shp.x >> shp.y >> shp.c;
v.push_back(shp);
++ a[shp.x][shp.x];
-- a[shp.x][shp.y];
++ a[shp.y][shp.y];
-- a[shp.y][shp.x];
}
for (int i = 1; i <= n; ++ i) {
a[1][i] = 0;
a[n][i] = 0;
}
a[1][1] = 1;
a[n][n] = a[n][n + 1] = 1;
gauss();
double rmin = INFINITY;
for (auto i : v) {
if (fabs(sol[i.y] - sol[i.x]) > eps) {
rmin = min(rmin, (double)i.c / fabs(sol[i.y] - sol[i.x]));
}
}
double ans = 0;
for (auto i : v) {
if (i.x == n || i.y == n)
ans += rmin * fabs(sol[i.x] - sol[i.y]);
}
fout << setprecision(6) << fixed;
fout << ans << "\n";
fin.close();
fout.close();
return 0;
}