Cod sursa(job #881423)

Utilizator laurionLaurentiu Ion laurion Data 17 februarie 2013 22:43:44
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.24 kb
#ifndef INFOARENA
#define DBG
#endif
#define nume "maxflow"

#include<cstdio>
#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
#include<string>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cassert>
#include<ctime>
#include<list>
#include<algorithm>

using namespace std;
using namespace __gnu_cxx;

//#define _PARSARE_
#ifdef _PARSARE_
#define DIM 8192
char ax[DIM+16];
int _idx;
template<class T>
inline void cit(T& x)
{
    x=0;
    while((ax[_idx]<'0' || ax[_idx]>'9') && (ax[_idx]!='-'))
        if(++_idx==DIM)fread(ax, 1, DIM, stdin), _idx=0;

    int neg=0;
    if(ax[_idx]=='-') {
        neg=1;
        if(++_idx==DIM)fread(ax, 1, DIM, stdin),_idx=0;
    }

    while(ax[_idx]>='0' && ax[_idx]<='9') {
        x=x*10+ax[_idx]-'0';
        if(++_idx==DIM)fread(ax,1, DIM, stdin),_idx=0;
    }
    if(neg) x=-x;
}
#else
#ifndef ONLINE_JUDGE
ifstream fin (nume ".in");
#endif
#endif //_PARSARE_
#ifndef ONLINE_JUDGE
ofstream fout(nume ".out");
#endif
#ifdef DBG
#define fout cout
#endif
//// hash-uri
//#include<ext/hash_map>
//hash_multimap<int,int> H;
//hash_map<char*,int> H;
//const double PI = acos(-1.0);
//const double EPS = 1e-9;
#define foreach(it, v) for (typeof((v).begin()) it = (v).begin(),stop=(v).end(); it != stop; ++it)
#define foreach_r(it, v) for (typeof((v).rbegin()) it = (v).rbegin(),stop=(v).rend(); it != stop; ++it)
template<class T> inline void Swap(T& a,T& b)
{
    a^=b,b^=a,a^=b;
}

int n,m;
vector<int> G[2000+3];
int C[1003][1003];
int F[1003][1003];
bool viz[1003];
int flux[1003];
int tata[1003];
int Stack[30000];
bool dfs()
{
    memset(viz,0,sizeof(viz));
    memset(Stack,0,sizeof(Stack));
    Stack[1]=1;
    Stack[0]=1;
    viz[1]=true;
    for(int sz=1;sz>0;) {
        int pos=rand()%(sz)+1;
        int nod=Stack[pos];
        swap(Stack[pos],Stack[sz--]);
        foreach(it,G[nod]) {
            if(!viz[*it] && C[nod][*it]-F[nod][*it]>0) {
                flux[*it] = min(flux[nod], C[nod][*it]-F[nod][*it]);
                viz[*it] = true;
                Stack[++sz]=*it;
                tata[*it] = nod;
                if(*it == n) {
                    for (int i=n; i!=1; i=tata[i]) {
                        F[tata[i]][i] += flux[n];
                        F[i][tata[i]] -= flux[n];
                    }
                    return true;
                }
            }
        }

    }
//    for (int i=n; i!=1; i=tata[i]) {
//        F[tata[i]][i] += flux[n];
//        F[i][tata[i]] -= flux[n];
//    }
//    return flux[n] != 0x3f3f3f3f;
    return false;
}
int main()
{
#ifdef _PARSARE_
#ifndef ONLINE_JUDGE
    freopen(nume ".in","r",stdin);
#endif
    cit(n);
#endif

    fin>>n>>m;
    for(int i=0; i<m; ++i) {
        int x,y,c;
        fin>>x>>y>>c;
        G[x].push_back(y);
        C[x][y]=c;
        G[y].push_back(x);
//        C[y][x]=0;
    }
    int maxflow=0;
    srand(time(0));
    while(true) {
        memset(flux,0x3f,sizeof(flux));
        if(!dfs()) break;
        maxflow+=flux[n];
    }
    fout<<maxflow<<'\n';

#ifndef DBG
#ifndef ONLINE_JUDGE
    fout.close();
#endif
#endif
    return 0;
}