Cod sursa(job #1165009)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 2 aprilie 2014 13:35:33
Problema Flux maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.02 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "fmcm.in";
const char outfile[] = "fmcm.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 355;
const int oo = 0x3f3f3f3f;

typedef vector<pair<int, int > > Graph[MAXN];
typedef vector<pair<int, int > > :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

const int lim = (1 << 20);
char buff[lim];
int pos;

inline void get(int &x) {
    x = 0;
    char sgn = '+';
    while(!('0' <= buff[pos] && buff[pos] <= '9')) {
        sgn = buff[pos];
        if(++ pos == lim) {
            pos = 0;
            fread(buff, 1, lim, stdin);
        }
    }
    while('0' <= buff[pos] && buff[pos] <= '9') {
        x = x * 10 + buff[pos] - '0';
        if(++ pos == lim) {
            pos = 0;
            fread(buff, 1, lim, stdin);
        }
    }
    if(sgn == '-')
        x = -x;
}

Graph G;
bitset <MAXN> inQ;
queue <int> Q;
int old[MAXN], real[MAXN], dp[MAXN], C[MAXN][MAXN], Father[MAXN], n, m, Source, Sink;
priority_queue <pair<int, int> , vector <pair<int, int> > , greater <pair<int, int> > > H;

inline void BellmanFord(Graph &G, int Source, int Sink) {
    memset(old, oo, sizeof(old));
    old[Source] = 0;
    Q.push(Source);
    while(!Q.empty()) {
        int Node = Q.front();
        Q.pop();
        inQ[Node] = 0;
        for(It it = G[Node].begin(), fin = G[Node].end() ; it != fin ; ++ it)
            if(C[Node][it->first] > 0 && old[it->first] > old[Node] + it->second) {
                old[it->first] = old[Node] + it->second;
                if(inQ[it->first])
                    continue;
                Q.push(it->first);
                inQ[it->first] = 1;
            }
    }
}

inline bool Dijkstra(Graph &G, int Source, int Sink) {
    memset(dp, oo, sizeof(dp));
    real[Source] = 0;
    dp[Source] = 0;
    H.push(make_pair(0, Source));
    while(!H.empty()) {
        int Node = H.top().second;
        int cst = H.top().first;
        H.pop();
        if(dp[Node] < cst)
            continue;
        for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) {
            if(C[Node][it->first])
                if(dp[it->first] > dp[Node] + it->second + old[Node] - old[it->first]) {
                    dp[it->first] = dp[Node] + it->second + old[Node] - old[it->first];
                    real[it->first] = real[Node] + it->second;
                    Father[it->first] = Node;
                    H.push(make_pair(dp[it->first], it->first));
                }
        }
    }
    memcpy(old, real, sizeof(real));
    return dp[Sink] != oo;
}

inline int getMinCostMaxFlow(Graph &G, int Source, int Sink) {
    int maxFlow = 0, minCostMaxFlow = 0;
    ///BellmanFord(G, Source, Sink);
    while(Dijkstra(G, Source, Sink)) {
        int bottleNeck = oo;
        for(int i = Sink ; i != Source ; i = Father[i])
            bottleNeck = min(bottleNeck, C[Father[i]][i]);
        for(int i = Sink ; i != Source ; i = Father[i]) {
            C[Father[i]][i] -= bottleNeck;
            C[i][Father[i]] += bottleNeck;
        }
        maxFlow += bottleNeck;
        minCostMaxFlow += bottleNeck * real[Sink];
    }
    return minCostMaxFlow;
}

int main() {
    freopen(infile, "r", stdin);
    get(n);
    get(m);
    get(Source);
    get(Sink);
    for(int i = 1 ; i <= m ; ++ i) {
        int x, y, z, c;
        get(x); get(y); get(z); get(c);
        G[x].push_back(make_pair(y, c));
        G[y].push_back(make_pair(x,-c));
        C[x][y] = z;
    }
    fout << getMinCostMaxFlow(G, Source, Sink) << '\n';
    fout.close();
    return 0;
}