Pagini recente » Cod sursa (job #2336931) | Cod sursa (job #2243067) | Cod sursa (job #2320980) | Cod sursa (job #902058) | Cod sursa (job #3139950)
#include <bits/stdc++.h>
using namespace std;
ifstream fin( "tunel.in" );
ofstream fout( "tunel.out" );
const int DIM = 256;
const double EPS = 1e-5;
double t[DIM][DIM];
double res[DIM];
vector<pair<int, int>> G[DIM];
int main() {
int n, m, u, v, c;
fin >> n >> m;
while ( m-- ) {
fin >> u >> v >> c;
G[u].push_back({v, c});
G[v].push_back({u, c});
}
for ( int i = 1; i < n; ++i ) {
for ( auto qq : G[i] ) {
--t[i][qq.first];
t[i][n + 1] += qq.second;
}
t[i][i] = G[i].size();
}
t[n][n] = 1;
for ( int l = 1, c = 1; l <= n && c <= n; ++c ) {
int nxt = l;
while ( nxt <= n && abs( t[nxt][c] ) < EPS ) {
++nxt;
}
if ( nxt > n ) continue;
for ( int i = 1; i <= n + 1; ++i ) {
swap( t[nxt][i], t[l][i] );
}
for ( int i = c + 1; i <= n + 1; ++i ) {
t[l][i] /= t[l][c];
}
t[l][c] = 1;
for ( int nl = l + 1; nl <= n; ++nl ) {
for ( int nc = c + 1; nc <= n + 1; ++nc ) {
t[nl][nc] -= t[nl][c] * t[l][nc];
}
t[nl][c] = 0;
}
++l;
}
for ( int i = n; i > 0; --i ) {
int j = 1;
while ( j <= n && abs( t[i][j] ) < EPS ) {
++j;
}
res[j] = t[i][n + 1];
for ( int nj = j + 1; nj <= n; ++nj ) {
res[j] -= res[nj] * t[i][nj];
}
}
fout << setprecision(4) << fixed << res[1];
fin.close();
fout.close();
return 0;
}