Cod sursa(job #1515013)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 31 octombrie 2015 23:36:24
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 1e3 + 5;

int n;
int C[NMax][NMax], Flow[NMax][NMax], Father[NMax];
bool Used[NMax];

vector < int > G[NMax];
deque < int > Q;

inline bool BFS(){
    vector < int > ::iterator it;
    int node;
    memset(Used, 1, sizeof(Used));
    Used[1] = 1;
    Q.clear();
    Q.push_back(1);
    while(!Q.empty()){
        node = Q.front(); Q.pop_front();
        for(it = G[node].begin(); it != G[node].end(); it++){
            if(C[node][*it] != Flow[node][*it] || !Used[*it]){
                Used[*it] = 1;
                Q.push_back(*it);
                Father[*it] = node;
                if(*it == n) return 1;
            }
        }
    }
    return 0;
}

int main(){
    int m, a, b, c, flowMin, flow;
    fin >> n >> m;
    for(int i = 1; i <= m; i++){
        fin >> a >> b >> c;
        C[a][b] += c;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for(flow = 0; BFS(); flow += flowMin){
        flowMin = INFINITY;
        for(int node = n; node != 1; node = Father[node]){
            flowMin = min(flowMin, C[Father[node]][node] - Flow[Father[node]][node]);
        }
        for(int node = n; node != 1; node = Father[node]){
            Flow[Father[node]][node] += flowMin;
            Flow[node][Father[node]] -= flowMin;
        }
    }
    fout << flow;
    return 0;
}