Cod sursa(job #624087)

Utilizator rootsroots1 roots Data 21 octombrie 2011 18:00:15
Problema Flux Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2 kb
#include <fstream>
#include <cstring>
#include <iomanip>
#include <cmath>

#define fSize 101
#define grafSize 101

#define eWidth 102
#define eHeight 101
#define costWidth 101
#define costHeight 101

using namespace std;

ifstream in;
ofstream out;

double e[eHeight][eWidth];
double f[fSize];

int cost[costHeight][costWidth];

inline double min(double a,double b)
{
    if(a<b) return a;
    else return b;
}

int main()
{
    int M,N,x,y;

    in.open("flux.in");

    in>>N>>M;
    for(int i=1;i<=M;++i)
    {
        in>>x>>y;
        in>>cost[x][y];
    }

    in.close();

    memset(f,0,sizeof(f));
    memset(e,0,sizeof(e));

    for(int i=2;i<=N;++i)
        for(int j=1;j<=N;++j)
        {
            if(cost[i][j]) e[i][j]=e[i][j]-1,e[i][i]=e[i][i]+1;
            if(cost[j][i]) e[i][j]=e[i][j]+1,e[i][i]=e[i][i]+1;
        }

    e[N][N+1]=-1;

    for(int row=2;row<=N;++row)
    {
        if(e[row][row]==0)
        {
            x=0;
            for(int i=row+1;i<=N;++i)
                if(e[i][i]!=0)
                {
                    x=i;
                    break;
                }

            for(int j=row;j<=N+1;++j)
            {
                double aux=e[row][j];
                e[row][j]=e[x][j];
                e[x][j]=aux;
            }
        }

        for(int i=row+1;i<=N+1;++i)
            e[row][i]/=e[row][row];
        e[row][row]=1;

        for(int i=row+1;i<=N;++i)
        {
            for(int j=row+1;j<=N+1;++j)
                e[i][j]-=e[i][row]*e[row][j];
            e[i][row]=0;
        }
    }

    for(int i=N;i>1;--i)
    {
        f[i]=e[i][N+1];
        for(int j=i+1;j<=N;++j)
            f[i]-=e[i][j]*f[j];
    }

    out.open("flux.out");

    double flux=10001;

    for(int i=1;i<=N;++i)
        for(int j=1;j<=N;++j)
            if(cost[i][j]) flux=min(flux,cost[i][j]/fabs(f[i]-f[j]));

    out<<fixed<<setprecision(3)<<flux<<'\n';

    out.close();

    return 0;
}