Pagini recente » Cod sursa (job #272126) | Cod sursa (job #3147102) | Cod sursa (job #3210817) | Cod sursa (job #199534) | Cod sursa (job #2589448)
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;
ifstream fin("tunel.in");
ofstream fout("tunel.out");
const int NMAX = 255;
const double EPS = 1e-6;
int N, M;
vector < pair <int, int> > g[NMAX + 5];
double sys[NMAX + 5][NMAX + 5];
double ans[NMAX + 5];
bool NotNull(double X)
{
return (X < -EPS || X > EPS);
}
int main()
{
fin >> N >> M;
for(int i = 1; i <= M; i++)
{
int x, y, z;
fin >> x >> y >> z;
g[x].push_back({y, z});
g[y].push_back({x, z});
}
for(int i = 1; i < N; i++)
{
sys[i][i] = g[i].size();
for(auto it : g[i])
{
sys[i][it.first] -= 1.0;
sys[i][N + 1] += it.second;
}
}
int l = 1, c = 1;
while(l <= N && c <= N)
{
int k;
for(k = l; k <= N; k++)
if(NotNull(sys[k][c]))
break;
if(k == N + 1) ///variabila libera, ii dam valoarea 0
{
c++;
continue;
}
///swap la liniile l, k
if(l != k)
for(int j = 1; j <= N + 1; j++)
swap(sys[l][j], sys[k][j]);
for(int j = c + 1; j <= N + 1; j++)
sys[l][j] /= sys[l][c];
sys[l][c] = 1;
for(int i = l + 1; i <= N; i++)
{
for(int j = c + 1; j <= N + 1; j++)
sys[i][j] -= sys[i][c] * sys[l][j];
sys[i][c] = 0;
}
l++, c++;
}
for(int i = N - 1; i >= 1; i--)
for(int j = 1; j <= N; j++)
if(NotNull(sys[i][j]))
{
ans[j] = sys[i][N + 1];
for(int c = j + 1; c <= N; c++)
ans[j] -= sys[i][c] * ans[c];
break;
}
fout << fixed << setprecision(6) << ans[1] << '\n';
return 0;
}