Cod sursa(job #1881167)

Utilizator DobosDobos Paul Dobos Data 16 februarie 2017 10:54:22
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <bits/stdc++.h>
#define NMAX 1005
#define INF 1e9
using namespace std;

ifstream fin("critice.in");
ofstream fout("critice.out");

struct point{
    int c,f;
};

vector < int > G[NMAX];
vector < pair < int , int > > E;

point F[NMAX][NMAX];
int ant[NMAX];
bool B[NMAX];

inline bool BFS(const int &N){
    int nod;
    memset(B,0,sizeof(B));
    B[1] = 1;
    queue < int > Q;
    Q.push(1);

    while(!Q.empty()){
        nod = Q.front();
        Q.pop();

        for(auto const &it : G[nod]){
            if(B[it] == 1 || F[nod][it].c == F[nod][it].f) continue;

            B[it] = 1;
            ant[it] = nod;
            if(it == N)
                return 1;
            Q.push(it);

        }
    }
    return 0;
}


int main()
{
    ios :: sync_with_stdio(false);
    fin.tie(NULL);

    int n,m,x,y,c,mflow;

    fin >> n >> m;

    for(int i = 1; i <= m; i++){
        fin >> x >> y >> c;

        G[x].push_back(y);
        G[y].push_back(x);
        F[x][y].c = c;
        F[y][x].c = c;
        E.push_back({x,y});

    }

    ant[1] = 1;

    while(BFS(n)){
        for(auto const &it : G[n]){

            if(B[it] == 0 || F[it][n].c ==  F[it][n].f) continue;

            mflow = INF;
            ant[n] = it;

            for(int j = n; j != 1; j = ant[j])
                mflow = min(mflow,F[ant[j]][j].c - F[ant[j]][j].f);

            if(mflow == 0) continue;

            for(int j = n; j != 1; j = ant[j]){

                F[ant[j]][j].f -= mflow;
                F[j][ant[j]].f += mflow;

            }

            fout << mflow << " ";

        }




    }


    return 0;
}