Cod sursa(job #1669707)

Utilizator andrew_assassin789Andrei Manea andrew_assassin789 Data 30 martie 2016 22:45:22
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.96 kb
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
#include <cstring>
#define nmax 1005
using namespace std;
unsigned int F[10001];
#define F (F + 5000)
unsigned int C[10001];
#define C (C + 5000)
unsigned int n;
bool viz[nmax];
class muchie
{
    public:
    unsigned short v;int nr;
};
queue <short> q;
vector <muchie> a[nmax];
muchie t[nmax];
void bf()
{
    memset(viz,0,sizeof(viz));
    unsigned int x,i;muchie y;
    viz[1]=1;q.push(1);
    while (!q.empty())
    {
        x=q.front();q.pop();
        if (x!=n)
        {
            for (i=0;i<a[x].size();i++)
            {
                y=a[x][i];
                if (C[y.nr]!=F[y.nr]&&(!viz[y.v]))
                {
                    q.push(y.v);
                    t[y.v].v=x;
                    t[y.v].nr=y.nr;
                    viz[y.v]=1;
                }
            }
        }
    }
}
int main()
{
    ifstream f("maxflow.in");
    ofstream g("maxflow.out");
    unsigned int m,i,x,y,z,flux=0,flm;
    muchie e;int nrm;
    f>>n>>m;
    for (i=1;i<=m;i++)
    {
        f>>x>>y>>z;
        e.v=y;e.nr=i;
        a[x].push_back(e);
        e.v=x;e.nr=-i;
        a[y].push_back(e);
        C[i]=z;C[-i]=z;
    }
    do
    {
        bf();
        for (i=0;i<a[n].size();i++)
        {
            x=a[n][i].v;nrm=a[n][i].nr;
            if (viz[x]&&F[nrm]!=C[nrm])
            {
                t[n].v=x;t[n].nr=nrm;
                flm=UINT_MAX;
                for (x=n;x!=1;x=t[x].v)
                {
                    nrm=t[x].nr;
                    flm=min(flm,C[nrm]-F[nrm]);
                }
                for (x=n;x!=1;x=t[x].v)
                {
                    nrm=t[x].nr;
                    F[nrm]+=flm;
                    F[-nrm]-=flm;
                }
                flux+=flm;
            }
        }
    }while (viz[n]);
    g<<flux<<'\n';
    f.close();
    g.close();
    return 0;
}