Cod sursa(job #968133)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 1 iulie 2013 20:28:50
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.71 kb
#include<cstdio>
#include<cstring>
#include<vector>
#include<deque>
using namespace std;
const int NMAX = 1005;
const int INF = 1<<30;
int N,M,X,Y,C,Cap[NMAX][NMAX],Flow[NMAX][NMAX],Father[NMAX],From,MaxFlow,MinFlow,Node;
vector<int> V[NMAX];
deque<int> Q;
void Read()
{
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    scanf("%d%d",&N,&M);
    for(;M;M--)
    {
        scanf("%d%d%d",&X,&Y,&C);
        V[X].push_back(Y); V[Y].push_back(X);
        Cap[X][Y]=C;
    }
}
void Init()
{
    memset(Father,0,sizeof(Father));
    Father[1]=1; Q.push_back(1);
}
int BFS()
{
    for(Init();!Q.empty();)
    {
        From=Q.front(); Q.pop_front(); 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_back(*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;
        }
    printf("%d\n",MaxFlow);
}
int main()
{
    Read();
    FindFlow();
    return 0;
}