Cod sursa(job #857188)

Utilizator mcip1977Muresan Ciprian mcip1977 Data 17 ianuarie 2013 15:42:35
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include<fstream>
#include<cstring>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

int C[1001][1001];//matricea costurilor
int n,m;
int T[1001];//vector TATA din BFS

int BFS() //returneaza 1 daca gaseste drum de crestere de la 1 la n
{
    int s=1,d=1,j,i,X[1001];
    memset(T,0,sizeof(T));
    memset(X,0,sizeof(X));
    X[1] = 1;T[1]=-1;
    while(s<=d)
    {   i=X[s];
        for(j=1;j<=n;j++){
            if(T[j]==0 && C[i][j]>0)
            {
                X[++d]=j;
                T[j]=i;
                if(j==n) return 1;
            }
        }
        s++;
    }
    return 0;
}

int flux_maxim()
{
    int i,minn,j,flux=0;
    while(BFS())//cat timp mai gaseste drumuri de crestere
      {
                minn=999999;
                j=n;
                while(j!=1)//calculeaza valoarea minima a drumului
                {
                    if(C[T[j]][j]<minn)
                        minn=C[T[j]][j];
                    j=T[j];
                }
                j=n;
                while(j!=1)//scade capacitatile pe drumul de crestere
                    {   C[T[j]][j]-=minn;
                        C[j][T[j]]+=minn;
                        j=T[j];
                    }
                 flux+=minn;//aduna minimul la fluxul maxim
      }
    return flux;
}

int main()
{
    int i,x,y,c;
    fin>>n>>m;
    for(i=0;i<=m;++i)
    {
        fin>>x>>y>>c;
        C[x][y]=c;
    }
    fout<<flux_maxim();
    return 0;
}