Pagini recente » Cod sursa (job #286532) | Cod sursa (job #2184214) | Cod sursa (job #350564) | Cod sursa (job #780979) | Cod sursa (job #2225939)
#include <bits/stdc++.h>
using namespace std;
const float eps = 1e-6;
const int MAXN = 255;
vector < pair <int, int> > g[MAXN + 1];
float eqs[MAXN + 1][MAXN + 2];
float dp[MAXN + 1];
int main() {
FILE *fi, *fout;
int i, n, m, j;
fi = fopen("tunel.in" ,"r");
fout = fopen("tunel.out" ,"w");
fscanf(fi,"%d %d " ,&n,&m);
for(i = 1; i <= m; i++) {
int a, b, c;
fscanf(fi,"%d %d %d " ,&a,&b,&c);
g[a].push_back({b, c});
g[b].push_back({a, c});
}
// dp[nod] = suma((1 / deg[nod]) * (dp[it.first] + it.second));
// dp[n] = 0;
for(int nod = 1; nod < n; nod++) {
int deg = g[nod].size();
for(auto it : g[nod]) {
eqs[nod][it.first] += 1.0;
eqs[nod][n + 1] -= 1.0 * it.second;
}
eqs[nod][nod] -= 1.0 * deg;
}
for(int nod = 1; nod < n; nod++) {
eqs[nod][n] = 0.0;
}
int l = 1, c = 1;
while(l < n && c <= n) {
i = l;
while(i <= n && fabs(eqs[i][c]) < eps) {
i++;
}
if(i == n + 1) {
c++;
}
else {
for(j = c; j <= n + 1; j++) {
swap(eqs[l][j], eqs[i][j]);
}
for(j = n + 1; j >= c; j--) {
eqs[l][j] /= eqs[l][c];
}
for(i = l + 1; i < n; i++) {
for(j = n + 1; j >= c; j--) {
eqs[i][j] -= eqs[l][j] * eqs[i][c];
}
}
l++;
c++;
}
}
for(i = n - 1; i >= 1; i--) {
j = 1;
while(j <= n && fabs(eqs[i][j]) < eps) {
j++;
}
dp[i] = eqs[i][n + 1];
for(int p = j + 1; p <= n; p++) {
dp[i] -= dp[p] * eqs[i][p];
}
}
fprintf(fout,"%.6f" ,dp[1]);
fclose(fi);
fclose(fout);
return 0;
}