Pagini recente » Istoria paginii utilizator/sosig | Monitorul de evaluare | Statistici Diaconu Diana (dianna) | Profil PiciuOlimpic | Cod sursa (job #2045896)
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#define EPS 0.00000001
FILE *fin = fopen("flux.in", "r");
FILE *fout = fopen("flux.out", "w");
#define MAXN 100
int c[MAXN + 1][MAXN + 1];
double a[MAXN + 1][MAXN + 1], v[MAXN + 1];
std::vector <int> g;
inline void gauss(int n) {
int c = 2, sef = n;
for (int i = 2; i < n; i++) {
int l = i;
while (l <= n && std::fabs(a[l][c]) < EPS)
l++;
if (l == n + 1) sef = c;
else {
if (l != i)
for (int j = c; j <= n; j++)
std::swap(a[l][j], a[i][j]);
for (int j = c + 1; j <= n; j++)
a[i][j] /= a[i][c];
a[i][c] = 1;
for (l = i + 1; l <= n; l++) {
for (int j = c + 1; j <= n; j++)
a[l][j] -= a[l][c] * a[i][j];
a[l][c] = 0;
}
}
c++;
}
v[sef] = 1;
for (int i = n; i > 1; i--)
for (int j = 2; j <= n; j++)
v[i] -= v[j] * a[i][j];
}
int main() {
int n, m;
fscanf(fin, "%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
c[i][j] = -1;
for (int i = 0; i < m; i++) {
int x, y, z;
fscanf(fin, "%d%d%d", &x, &y, &z);
if (c[x][y] == -1 || c[x][y] > z)
c[x][y] = z, c[y][x] = z;
a[x][y]++;
a[x][x]--;
a[y][x]++;
a[y][y]--;
if (x == 1)
g.push_back(y);
else if (y == 1)
g.push_back(x);
}
gauss(n);
double t = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (c[i][j] != -1 && std::fabs(v[i] - v[j]) > EPS)
if (t < -EPS || c[i][j] < std::fabs(v[i] - v[j]) * t)
t = c[i][j] / std::fabs(v[i] - v[j]);
double ans = 0;
if (t > -10)
for (auto &x : g)
ans += t * v[x];
fprintf(fout, "%.3f\n", ans);
fclose(fin);
fclose(fout);
return 0;
}