Cod sursa(job #875291)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 9 februarie 2013 21:22:21
Problema Flux maxim de cost minim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 4.05 kb
#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

typedef vector<int>::iterator it;

const int MaxN = 355;
const int NIL = -1;
const int oo = 0x3f3f3f3f;

vector<int> G[MaxN];
int N, Source, Sink, Capacity[MaxN][MaxN], Cost[MaxN][MaxN];
int Father[MaxN], Distance[MaxN], Flow[MaxN][MaxN];
int TimesInQ[MaxN];
bool InQ[MaxN], Used[MaxN];
queue<int> Q;
int Solution;

void InitBFS(int Start) {
    memset(Father, NIL, sizeof(Father));
    Q.push(Start), Father[Start] = Start;
}

bool BFS(int Start, int End) {
    for (InitBFS(Start); !Q.empty(); Q.pop()) {
        int X = Q.front();
        if (X == End)
            continue;
        for (it Y = G[X].begin(); Y != G[X].end(); ++Y)
            if (Father[*Y] == NIL && Capacity[X][*Y] > Flow[X][*Y])
                Q.push(*Y), Father[*Y] = X;
    }
    return (Father[End] != NIL);
}

int FordFulkerson() {
    int MaxFlow = 0;
    while (BFS(Source, Sink)) {
        for (it Y = G[Sink].begin(); Y != G[Sink].end(); ++Y) {
            if (Father[*Y] == NIL || Capacity[*Y][Sink] <= Flow[*Y][Sink])
                continue;
            Father[Sink] = *Y;
            int CurrentFlow = oo;
            for (int X = Sink; X != Source; X = Father[X])
                CurrentFlow = min(CurrentFlow, Capacity[Father[X]][X] - Flow[Father[X]][X]);
            for (int X = Sink; X != Source; X = Father[X])
                Flow[Father[X]][X] += CurrentFlow, Flow[X][Father[X]] -= CurrentFlow;
            MaxFlow += CurrentFlow;
        }
    }
    return MaxFlow;
}

void InitBF(int Start) {
    memset(Distance, oo, sizeof(Distance));
    memset(TimesInQ, 0, sizeof(TimesInQ));
    Q.push(Start), InQ[Start] = true, Father[Start] = Start, Distance[Start] = 0;
}

int BellmanFord(int Start) {
    for (InitBF(Start); !Q.empty(); Q.pop()) {
        int X = Q.front(); InQ[X] = false;
        if (TimesInQ[X] >= N)
            return X;
        for (it Y = G[X].begin(); Y != G[X].end(); ++Y) {
            if (Capacity[X][*Y] > Flow[X][*Y] && Distance[X] + Cost[X][*Y] < Distance[*Y]) {
                Distance[*Y] = Distance[X] + Cost[X][*Y], Father[*Y] = X;
                ++TimesInQ[*Y];
                if (!InQ[*Y]) {
                    Q.push(*Y), InQ[*Y] = true;
                }
            }
        }
    }
    return NIL;
}

int FindCycle() {
    memset(Father, NIL, sizeof(Father));
    memset(Used, 0, sizeof(Used));
    for (int X = 1, Cycle; X <= N; ++X) {
        if (Father[X] == NIL) {
            if ((Cycle = BellmanFord(X)) != NIL) {
                for (; !Used[Cycle]; Cycle = Father[Cycle])
                    Used[Cycle] = true;
                return Cycle;
            }
        }
    }
    return NIL;
}

int MaximumFlow() {
    int MaxFlow, FlowCost = 0;
    MaxFlow = FordFulkerson();
    int Start, X;
    while ((Start = FindCycle()) != NIL) {
        int CurrentFlow = oo;
        X = Start;
        do {
            CurrentFlow = min(CurrentFlow, Capacity[Father[X]][X] - Flow[Father[X]][X]);
            X = Father[X];
        } while (X != Start);
        X = Start;
        do {
            Flow[Father[X]][X] += CurrentFlow, Flow[X][Father[X]] -= CurrentFlow;
            X = Father[X];
        } while (X != Start);
    }
    for (int X = 1; X <= N; ++X)
        for (int Y = 1; Y <= N; ++Y)
            FlowCost += (Flow[X][Y] > 0 ? Flow[X][Y] * Cost[X][Y] : 0);
    return FlowCost;
}

void Read() {
    assert(freopen("fmcm.in", "r", stdin));
    int M; assert(scanf("%d %d %d %d", &N, &M, &Source, &Sink) == 4);
    for (; M > 0; --M) {
        int X, Y; assert(scanf("%d %d", &X, &Y) == 2);
        G[X].push_back(Y), G[Y].push_back(X);
        assert(scanf("%d %d", &Capacity[X][Y], &Cost[X][Y]) == 2);
        Cost[Y][X] = -Cost[X][Y];
    }
}

void Print() {
    assert(freopen("fmcm.out", "w", stdout));
    printf("%d\n", Solution);
}

int main() {
    Read();
    Solution = MaximumFlow();
    Print();
    return 0;
}