Pagini recente » Cod sursa (job #476893) | Cod sursa (job #451154) | Cod sursa (job #1067972) | Cod sursa (job #396846) | Cod sursa (job #1522354)
#include <bits/stdc++.h>
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define mod 666013
#define pb push_back
#define mp make_pair
#define eps 0.00000001
using namespace std;
typedef pair<int, int> pereche;
int n, m, i, j, k, x, y, z;
int st[270];
double coef, sol;
double a[270][270];
vector <pereche> v[270];
vector <pereche> :: iterator it;
int main()
{
freopen("tunel.in", "r", stdin);
freopen("tunel.out", "w", stdout);
scanf("%d%d", &n, &m);
while(m--)
{
scanf("%d%d%d", &x, &y, &z);
v[x].pb(mp(y, z));
v[y].pb(mp(x, z));
}
for(i = 1; i < n; i++)
if(v[i].size())
{
coef = (double)1 / (double)v[i].size();
for(it = v[i].begin(); it != v[i].end(); it++)
{
a[i][(*it).first] += coef;
a[i][n + 1] -= coef * (*it).second;
}
a[i][i] = -1;
}
for(i = 1; i <= n; i++)
a[i][n] = 0;
for(i = 1; i <= n; i++)
{
for(st[i] = 1; st[i] <= n + 1 && fabs(a[i][st[i]]) < eps; st[i]++);
if(st[i] == n + 1)
{
printf("-1");
return 0;
}
if(st[i] > n + 1)
{
st[i] = 0;
continue;
}
for(j = 1; j <= n; j++)
{
if(i == j || fabs(a[j][st[i]]) < eps)
continue;
coef = a[j][st[i]] / a[i][st[i]];
for(k = 1; k <= n + 1; k++)
a[j][k] -= a[i][k] * coef;
}
}
sol = a[1][n + 1] / a[1][1];
printf("%.3f", sol);
return 0;
}