Pagini recente » Cod sursa (job #3250279) | Cod sursa (job #3247927) | Cod sursa (job #2415627) | Cod sursa (job #279908) | Cod sursa (job #3185416)
#include <bits/stdc++.h>
using namespace std;
#ifndef HOME
ifstream in("tunel.in");
ofstream out("tunel.out");
#define cin in
#define cout out
#endif
long double coef[256][256];
const long double eps = 1e-8;
int main()
{
#ifdef HOME
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
///dp[i] = timpul mediu de a ajunge din i in n
///dp[i] = suma(dp[j] + cost[i][j] | i are muchie cu j) / grad[i] | *grad[i]
///dp[i] * grad[i] = suma(dp[j] | i are muchie cu j) + sumcost[i]
///grad[i] * dp[i] - suma(dp[j] | i are muchie cu j) = sumcost[i]
int n, m, a, b, c;
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> a >> b >> c;
///pentru a
coef[a][a]++;
coef[a][b]--;
coef[a][n + 1] += c;
///pentru b
coef[b][b]++;
coef[b][a]--;
coef[b][n + 1] += c;
}
for(int j = 1; j <= n - 1; j++)
coef[n][j] = 0;
coef[n][n] = 1;
coef[n][n + 1] = 0;
int lastEq = 1;
for(int j = 1; j <= n; j++)
{
for(int i = lastEq; i <= m; i++)
if(coef[i][j] >= eps || coef[i][j] <= -eps)
{
for(int k = 1; k <= n + 1; k++)
swap(coef[lastEq][k], coef[i][k]);
break;
}
if(coef[j][j] >= eps || coef[j][j] <= -eps)
{
long double x = coef[lastEq][j];
for(int k = 1; k <= n + 1; k++)
coef[lastEq][k] /= x;
for(int i = 1; i <= m; i++)
if(i != j)
{
long double x = coef[i][j];
for(int k = 1; k <= n + 1; k++)
coef[i][k] -= coef[lastEq][k] * x;
/// bk -= ak * bj
}
lastEq++;
}
}
cout << fixed << setprecision(6) << coef[1][n + 1];
return 0;
}