Cod sursa(job #1739084)

Utilizator c0mradec0mrade c0mrade Data 8 august 2016 16:24:49
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include<bits/stdc++.h>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");

int N, M, x, y, z, flow, max_flow, nod;
int f[1111], c[1111][1111];
bitset<1111>viz;
vector<int> v[1111];
queue<int> q;

int bfs()
{
    memset(f,0,sizeof(f));
    viz.reset();
    q.push(1);
    viz[1] = 1;
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        viz[x] = 1;
        for(int i = 0; i < v[x].size(); ++i)
        {
            if(!viz[v[x][i]] && c[x][v[x][i]])
            {
                viz[v[x][i]] = 1;
                f[v[x][i]] = x;
                q.push(v[x][i]);
            }
        }
    }
    return viz[N];
}

int main()
{
    in >> N >> M;
    for(int i = 0; i < M; ++i)
    {
        in >> x >> y >> z;
        v[x].push_back(y);
        v[y].push_back(x);
        c[x][y] = z;
    }

    while(bfs())
    {
        nod = N;
        flow = 111111;
        while(nod != 1)
        {
            flow = min(flow, c[f[nod]][nod]);
            nod = f[nod];
        }
        nod = N;
        while(nod != 1)
        {
            c[f[nod]][nod] -= flow;
            c[nod][f[nod]] += flow;
            nod = f[nod];
        }
        max_flow += flow;
    }
    out<<max_flow;
    return 0;
}