Cod sursa(job #1069915)

Utilizator Dddarius95Darius-Florentin Neatu Dddarius95 Data 30 decembrie 2013 17:44:55
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include<fstream>
#include<vector>
#include<queue>
#include<string.h>
#define Nmax 1009
#define Inf (1<<30)
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");

int N,M,X,Y,C,Cap[Nmax][Nmax],Flow[Nmax][Nmax],Father[Nmax],From,MaxFlow,MinFlow,Node;
vector<int> V[Nmax];
queue<int> Q;

void Read()
{

    f>>N>>M;
    for(int i=1;i<=M;++i)
    {
        int x,y,c;
        f>>x>>y>>c;
        V[x].push_back(y);
        V[y].push_back(x);
        Cap[x][y]=c;
    }
}

int BFS()
{
    memset(Father,0,sizeof(Father));
    Father[1]=1; Q.push(1);
    for(;!Q.empty();)
    {
        From=Q.front(); Q.pop(); if(From==N) continue;
        for(vector<int>::iterator it=V[From].begin();it!=V[From].end();it++)
            if(!Father[*it] && Flow[From][*it]<Cap[From][*it])
            {
                Father[*it]=From;
                Q.push(*it);
            }
    }
    return Father[N];
}
void FindFlow()
{
    for(;BFS();)
        for(vector<int>::iterator it=V[N].begin();it!=V[N].end();it++)
        {
            if(!Father[*it] || Flow[*it][N]==Cap[*it][N]) continue;
            Father[N]=*it; MinFlow=Inf;
            for(Node=N;Node!=Father[Node];Node=Father[Node])
                MinFlow=min(MinFlow,Cap[Father[Node]][Node]-Flow[Father[Node]][Node]);
            if(!MinFlow) continue;
            for(Node=N;Node!=Father[Node];Node=Father[Node])
            {
                Flow[Father[Node]][Node]+=MinFlow;
                Flow[Node][Father[Node]]-=MinFlow;
            }
            MaxFlow+=MinFlow;
        }
    g<<MaxFlow<<'\n';
}
int main()
{
    Read();
    FindFlow();
    f.close();g.close();
    return 0;
}