Cod sursa(job #1402196)

Utilizator casianos1996Marc Casian Nicolae casianos1996 Data 26 martie 2015 13:26:38
Problema Problema Damelor Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>
#define pb push_back
#define INF numeric_limits<int>::max()
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
vector< vector<int> > a;
queue<int> q;
int flow[1005][1005],n,k,s,d,maxflow,pre[1005],t[1005];
//..................
bool bfs()
{
    for(int i=1;i<=n;i++)pre[i]=0;
    k=0;
    pre[s]=s;
    q.push(s);
    while(!q.empty())
    {
        int x=q.front();q.pop();
        for(auto i: a[x])
        if(pre[i]==0 && flow[x][i]>0)
        {
            if(i==d)
            {
                t[++k]=x;
                continue;
            }
            pre[i]=x;
            q.push(i);
        }
    }
    return k;
}
//....................
int main()
{
    int m;
    in>>n>>m;
    a=vector< vector<int> > (n+1);
    for(;m;m--)
    {
        int x,y,z;
        in>>x>>y>>z;
        a[x].pb(y);
        a[y].pb(x);
        flow[x][y]=z;
    }
    //.....................
    for(s=1,d=n;bfs();)
    {
        for(int i=1;i<=k;i++)
        {
            int mx=INF;
            pre[d]=t[i];

            for(int x=d;pre[x]!=x;x=pre[x])
                mx=min(mx,flow[pre[x]][x]);

            for(int x=d;pre[x]!=x;x=pre[x])
            {
                flow[pre[x]][x]-=mx;
                flow[x][pre[x]]+=mx;
            }

            maxflow+=mx;
        }
    }
    //.....................
    out<<maxflow<<'\n';
    return 0;
}