Cod sursa(job #1333868)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 3 februarie 2015 17:44:34
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.09 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>

#define Nmax 1010
#define oo (1 << 30)
#define neighbour Graph[node][i]

using namespace std;

vector <int> Graph[Nmax];
int N, maxFlow, Source, Destination, Father[Nmax], Capacity[Nmax][Nmax];
bool used[Nmax];

bool Bfs() {

    int i, node;
    queue <int> Queue;

    memset(used, 0, sizeof(used));
    Queue.push(Source);
    used[Source] = true;

    while(!Queue.empty()) {

        node = Queue.front();
        Queue.pop();

        for(i = 0; i < Graph[node].size(); i++)
            if(!used[neighbour] && Capacity[node][neighbour] > 0) {

                Queue.push(neighbour);
                used[neighbour] = true;
                Father[neighbour] = node;

                if(neighbour == Destination)
                    return true;
            }
    }

    return false;
}
void Solve() {

    int i, node, maxCapacity;

    Source = 1;
    Destination = N;

    while(Bfs())
        for(i = 0; i < Graph[Destination].size(); i++)
            if(used[node = Graph[Destination][i]] && Capacity[node][Destination] > 0) {

                maxCapacity = oo;
                Father[Destination] = node;

                for(node = Destination; node != Source; node = Father[node])
                    maxCapacity = min(maxCapacity, Capacity[Father[node]][node]);

                for(node = Destination; node != Source; node = Father[node]) {
                    Capacity[Father[node]][node] -= maxCapacity;
                    Capacity[node][Father[node]] += maxCapacity;
                }

                maxFlow += maxCapacity;
            }

}
void Read() {

    int x, y, c, M;
    ifstream in("maxflow.in");
    in >> N >> M;

    while(M--) {
        in >> x >> y >> c;
        Graph[x].push_back(y);
        Graph[y].push_back(x);
        Capacity[x][y] = c;
    }

    in.close();
}
void Write() {

    ofstream out("maxflow.out");
    out << maxFlow << '\n';
    out.close();
}
int main() {

    Read();
    Solve();
    Write();

    return 0;
}