Pagini recente » Cod sursa (job #2238207) | Cod sursa (job #2632976) | Cod sursa (job #2128340) | Cod sursa (job #1133548) | Cod sursa (job #2604296)
#include <fstream>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;
ifstream fin("tunel.in");
ofstream fout("tunel.out");
const int NMax = 255;
const double EPS = 0.001;
int N, M, Grade[NMax + 5], SumC[NMax + 5];
double A[NMax + 5][NMax + 5], X[NMax + 5];
vector <int> G[NMax + 5];
int main()
{
fin >> N >> M;
for(int i = 1, x, y, c; i <= M; i++)
{
fin >> x >> y >> c;
Grade[x]++, Grade[y]++;
SumC[x] += c, SumC[y] += c;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1; i < N; i++)
{
A[i][i] = -Grade[i];
for(auto Nei : G[i])
A[i][Nei] += 1;
A[i][N + 1] = -SumC[i];
}
///X[N] = 0
A[N][N] = 1;
int i = 1, j = 1, k;
double ct;
while(i <= N && j <= N)
{
for(k = i; k <= N; k++)
if(fabs(A[k][j]) > EPS)
break;
if(k == N + 1)
{
j++;
continue;
}
swap(A[i], A[k]);
ct = A[i][j];
for(k = j; k <= N + 1; k++)
A[i][k] /= ct;
for(int x = i + 1; x <= N; x++)
{
ct = A[x][j];
for(int y = j; y <= N + 1; y++)
A[x][y] -= ct * A[i][y];
}
i++, j++;
}
for(i = N; i > 0; i--)
for(j = 1; j <= N; j++)
if(fabs(A[i][j]) > EPS)
{
X[j] = A[i][N + 1];
for(k = j + 1; k <= N; k++)
X[j] -= X[k] * A[i][k];
break;
}
fout << fixed << setprecision(5) << X[1] << '\n';
fin.close();
fout.close();
return 0;
}