Cod sursa(job #113338)

Utilizator mariusdrgdragus marius mariusdrg Data 9 decembrie 2007 18:09:37
Problema Tunelul groazei Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include<stdio.h>
#include<algorithm>

using namespace std;

const int maxn = 260;

int i,n,j;
double coef[maxn][maxn];
int k;
int m,x,y,c,grad[maxn];

void da_cu_gauss_de_pereti(void)
{
        for(i = 1;i <= n; ++i)
        {
                if (i == n) return ;
                for(j = i;coef[i][j] == 0; ++j);
                if (j != i)
                {
                        for(k = 1;k <= n + 1; ++k)
                              swap(coef[i][k],coef[j][k]);
                }
                for(j = 1;j <= n; ++j)
                {
                        if (j == i) continue;
                        double rap = coef[j][i] / coef[i][i];
                        for(k = 1;k <= n + 1; ++k)
                        {
                                coef[j][k] -= (coef[i][k] * rap);
                        }
                }
        }
}


int main()
{
        freopen("tunel.in","r",stdin);
        freopen("tunel.out","w",stdout);
        scanf("%d %d",&n,&m);
        for(i = 1;i <= m; ++i)
        {
                scanf("%d %d %d",&x,&y,&c);
                coef[x][y] -= 1;
                grad[x]++;
                coef[x][n + 1] += c;
                coef[y][x] -= 1;
                grad[y]++;
                coef[y][n + 1] += c;
        }
        for(i = 1;i <= n; ++i)
        {
                coef[i][i] = (double)grad[i];
        }
        da_cu_gauss_de_pereti();
        printf("%.5lf",(double) coef[1][n + 1] / coef[1][1]);
        /*for(i = 1;i <= n; ++i)
        {
                printf("%lf",&coef[i][n + 1] / )
        } */
        return 0;
}