Pagini recente » Profil Legion | Cod sursa (job #2012504) | Diferente pentru utilizator/stargold2 intre reviziile 275 si 111 | Statistici Stanciu-Tivlea Valentin Gabriel (bitza1247) | Cod sursa (job #2023337)
#include<bits/stdc++.h>
#define maxN 105
#define maxM 5005
#define eps 0.00000001
using namespace std;
long double a[maxN][maxN];
typedef struct tip
{
int x,y,c;
};
int seen[maxN],cnt;
tip edge[maxM];
vector<pair<int,int> > v[maxN];
inline void dfs(int nod)
{
seen[nod]=++cnt;
for(vector<pair<int,int> >::iterator it=v[nod].begin();it!=v[nod].end();it++)
{
if(!seen[it->first]) dfs(it->first);
}
}
int n,m,x,y,c;
long double sol[maxN],ans;
ifstream fin("flux.in");
ofstream fout("flux.out");
int main()
{
// freopen("flux.in","r",stdin);
// freopen("flux.out","w",stdout);
// scanf("%d%d",&n,&m);
fin>>n>>m;
for(int i=1;i<=m;i++)
{
// scanf("%d%d%d",&x,&y,&c);
fin>>x>>y>>c;
v[x].push_back({y,c});
v[y].push_back({x,c});
edge[i]={x,y,c};
}
dfs(1);
/*if(!seen[n])
{
// printf("%lf\n",0.0);
fout<<'0.0'<<'\n';
return 0;
}*/
a[1][1]=a[n][n]=1.0;
a[n][n+1]=1.0;
//gauss[1][n+1]=1.0;
sol[n]=1.0;
for(int i=2;i<n;i++)
{
// if(i==seen[n]) continue;
a[i][i]=1.0*v[i].size();
for(vector<pair<int,int> >::iterator it=v[i].begin();it!=v[i].end();it++)
{
a[i][it->first]-=1.0;
}
}
// sol[n]=1.0;
int i=1,j=1;
while(i<=n && j<=n)
{
int k;
for(k=i;k<=n;k++)
{
if(a[k][j]<-eps || a[k][j]>eps)
break;
}
if(k==(n+1)) //Necunoscuta Libera
{
j++;
continue;
}
if(k!=i)
{
//Schimbam liniile k si i
for(int u=1;u<=n+1;u++)
{
swap(a[i][u],a[k][u]);
}
}
for(int u=j+1;u<=n+1;u++)
{
a[i][u]/=a[i][j];
}
a[i][j]=1; //Reducem toata ecuatia de pe linia i ca sefii
for(int u=i+1;u<=n;u++)
{
for(int l=j+1;l<=n+1;l++)
{
a[u][l]-=a[u][j]*a[i][l];
}
a[u][j]=0; //Practic reducem necunoscuta i din toate ecuatiile
}
i++;
j++;
}
for(int i=n;i>0;i--)
{
for(int j=1;j<=n+1;j++)
{
if(a[i][j]<-eps || a[i][j]>eps)
{
if(j==n+1)
{
printf("Imposibil\n");
return 0;
}
sol[j]=a[i][n+1];
for(int k=j+1;k<=n;k++)
sol[j]-=sol[k]*a[i][k];
break;
}
}
}
long double coef=1E60;
// cerr<<eps<<'\n';
for(int i=1;i<=m;i++)
{
if(edge[i].x && edge[i].y) coef=min(coef,(1.0*edge[i].c)/fabs(sol[edge[i].y]-sol[edge[i].x]));
}
for(vector<pair<int,int> >::iterator it=v[1].begin();it!=v[1].end();it++)
{
ans=ans+(sol[it->first]*coef);
}
fout<<setprecision(10)<<ans<<'\n';
return 0;
}