Cod sursa(job #2006092)

Utilizator mihai.alphamihai craciun mihai.alpha Data 28 iulie 2017 18:53:32
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <bits/stdc++.h>

using namespace std;

int n, m;
vector <int> v[1001];
int cap[1001][1001], flx[1001][1001];

int bst;

bool viz[1001];
int pre[1001];

bool BFS()  {
    queue <int> q;
    bst = INT_MAX;
    memset(viz, 0, sizeof(viz));
    q.push(1);
    viz[1] = 1;
    bool st = 1;
    while(!q.empty() && st)  {
        int nod = q.front();
        q.pop();
        for(int i = 0;i < v[nod].size() && st;i++)  {
            int x = v[nod][i];
            if(cap[nod][x] != flx[nod][x] && viz[x] == 0)  {
                viz[x] = 1;
                q.push(x);
                pre[x] = nod;
                if(x == n)  {
                    int nod1 = n;
                    bst = INT_MAX;
                    while(nod1 != 1)  {
                        bst = min(bst, cap[pre[nod1]][nod1] - flx[pre[nod1]][nod1]);
                        nod1 = pre[nod1];
                    }
                    nod1 = n;
                    while(nod1 != 1)  {
                        flx[pre[nod1]][nod1] += bst;
                        flx[nod1][pre[nod1]] -= bst;
                        nod1 = pre[nod1];
                    }
                    return 1;
                }
            }
        }
    }
    return 0;
}

int main()  {
    freopen("maxflow.in", "r", stdin);
    freopen("maxflow.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i = 1;i <= m;i++)  {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        v[x].push_back(y);
        v[y].push_back(x);
        cap[x][y] += z;
    }
    int ans = 0;
    while(BFS())  {
        ans += bst;
    }
    printf("%d", ans);
    return 0;
}