Pagini recente » Cod sursa (job #876162) | Cod sursa (job #2595968) | Cod sursa (job #2436564) | Cod sursa (job #2436566) | Cod sursa (job #996735)
Cod sursa(job #996735)
#include<fstream>
#include<vector>
#include<cmath>
#include<iomanip>
#define pb push_back
using namespace std;
const double ep=1e-6;
double a[300][300],p;
vector <int> b[300],c[300];
int i,n,m,j,k,x,y,z;
void sw(int x,int y)
{
int i;
for (i=1;i<=n;i++)
swap(a[x][i],a[y][i]);
}
int main()
{
ifstream f("tunel.in");
ofstream g("tunel.out");
f>>n>>m;
for (i=1;i<=m;i++)
{
f>>x>>y>>z;
b[x].pb(y);
b[y].pb(x);
c[x].pb(z);
c[y].pb(z);
}
a[n][n]=1;
for (i=1;i<n;i++)
{
a[i][i]=b[i].size();
for (j=0;j<b[i].size();j++)
{
a[i][b[i][j]]-=1;
a[i][n+1]+=c[i][j];
}
}
for (i=1;i<=n;i++)
{
if (fabs(a[i][i])<ep)
for (j=i+1;j<=n;j++)
if (fabs(a[j][i])>ep)
{
sw(i,j);
break;
}
for (j=1;j<=n;j++)
if (i!=j)
{
p=a[j][i]/a[i][i];
for (k=1;k<=n+1;k++)
a[j][k]-=a[i][k]*p;
}
}
g<<setprecision(6)<<fixed<<a[1][n+1]/a[1][1];
return 0;
}