Cod sursa(job #1888132)

Utilizator mariakKapros Maria mariak Data 21 februarie 2017 22:29:34
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.73 kb
#include <bits/stdc++.h>
#define maxN 1002

FILE *fin  = freopen("maxflow.in", "r", stdin);
FILE *fout = freopen("maxflow.out", "w", stdout);

using namespace std;
int N, M, S, D;
int r[maxN][maxN];
int flow;
queue <int> Q;
struct Node{
    int dad;
    vector <int> adj;
} G[maxN];

void read(){
    int u, v, capacity;
    scanf("%d %d", &N, &M);
    for(int i = 0; i < M; ++ i){
        scanf("%d %d %d", &u, &v, &capacity);
        G[u].adj.push_back(v);
        G[v].adj.push_back(u);
        r[u][v] = capacity;
    }
    S = 1, D = N;
}

bool bfs(){
    for(int i = 1; i <= N; ++ i)
        G[i].dad = 0;
    Q.push(S);
    G[S].dad = S;

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

        for(int son : G[node].adj){
            if(r[node][son] == 0 || G[son].dad) continue;
            if(son != D) /// very important
                Q.push(son);
            G[son].dad = node;
        }
    }
    return G[D].dad != 0;
}

void solve(){
    while(bfs()){
        for(int node : G[N].adj){
            if(G[node].dad){
                G[D].dad = node;
                int pathFlow = 0x7fffffff;
                node = D;
                while(node != S){
                    pathFlow = min(pathFlow, r[G[node].dad][node]);
                    node = G[node].dad;
                }
                node = D;
                while(node != S){
                    r[G[node].dad][node] -= pathFlow;
                    r[node][G[node].dad] += pathFlow;
                    node = G[node].dad;
                }
                flow += pathFlow;
            }
        }
    }
    printf("%d\n", flow);
}

int main()
{
    read();
    solve();
    return 0;
}