Cod sursa(job #1216251)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 3 august 2014 21:53:30
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.38 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 = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<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; }

class DirectedGraph {
public:
    DirectedGraph() {

    }
    DirectedGraph(int N) {
        g.resize(N);
        father.resize(N);
        dp.resize(N);
        inq.resize(N);
        _cap.resize(N, vector <int>(N));
    }
    void addEdge(int x, int y, int capa, int cost) {
        g[x].push_back(make_pair(y, cost));
        g[y].push_back(make_pair(x,-cost));
        _cap[x][y] += capa;
    }
    bool BellmanFord(int Source, int Sink) {
        fill(dp.begin(), dp.end(), oo);
        q.push(Source);
        inq[Source] = 1;
        dp[Source] = 0;
        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(dp[it->first] > dp[node] + it->second && _cap[node][it->first] > 0) {
                    dp[it->first] = dp[node] + it->second;
                    father[it->first] = node;
                    if(inq[it->first])
                        continue;
                    q.push(it->first);
                    inq[it->first] = 1;
                }
            }
        }
        return dp[Sink] != oo;
    }
    int getMinCostMaxFlow(int Source, int Sink) {
        int minCostMaxFlow = 0;
        while(BellmanFord(Source, Sink)) {
            int bottleNeck = oo;
            for(int i = Sink ; i != Source ; i = father[i])
                bottleNeck = min(bottleNeck, _cap[father[i]][i]);
            if(!bottleNeck)
                continue;
            for(int i = Sink ; i != Source ; i = father[i]) {
                _cap[father[i]][i] -= bottleNeck;
                _cap[i][father[i]] += bottleNeck;
            }
            minCostMaxFlow += bottleNeck * dp[Sink];
        }
        return minCostMaxFlow;
    }
private:
    vector <vector <pair<int, int> > > g;
    vector <vector <int> > _cap;
    /// BelmanFord
    vector <bool> inq;
    vector <int> dp, father;
    queue <int> q;
} G;

int main() {
    cin.sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);
    #endif
    int n, m, source, sink;
    fin >> n >> m >> source >> sink;
    G = DirectedGraph(n);
    for(int i = 1 ; i <= m ; ++ i) {
        int x, y, cap, cost;
        fin >> x >> y >> cap >> cost;
        G.addEdge(x-1, y-1, cap, cost);
    }
    fout << G.getMinCostMaxFlow(source - 1, sink - 1) << '\n';
    fin.close();
    fout.close();
    return 0;
}