Cod sursa(job #1955904)

Utilizator bt.panteaPantea Beniamin bt.pantea Data 6 aprilie 2017 12:34:54
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.34 kb
#include "iostream"
#include "fstream"
#include "vector"
#include "map"
#include <bitset>
#include <set>
#include <cstring>
#include <queue>
#define NMAX 1005
#define INF 1000000000
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
int flux[NMAX][NMAX], n, m;
int Father[NMAX], frezmin, MinFather[NMAX];
int bf[NMAX], cost[NMAX][NMAX];
vector < int > a[NMAX];

inline int min(const int & a, const int & b) {
    if (a < b) return a;
    return b;
}

bitset < NMAX > inqueue;
int old[NMAX];
queue < int > Q;

inline void bellman(const int & source, const int & destination)
{
    memset(old, 0x3f, sizeof(old));
    Q.push(source);
    old[source] = 0;
    inqueue[source] = 1;
    while (!Q.empty())
    {
        int nod = Q.front();
        inqueue[nod] = 0;
        Q.pop();
        for (int i = 0; i < a[nod].size(); i++) {
            int next = a[nod][i];
            if (flux[nod][next] > 0) {
                if (old[nod] + cost[nod][next] >= old[next]) continue;
                old[next] = old[nod] + cost[nod][next];
                if (inqueue[next]) continue;
                inqueue[next] = 1;
                Q.push(next);
            }
        }
    }
}




inline bool BF(const int & source, const int & destination) {
    set < pair < int, int > > myset;
    bitset < NMAX > used;
    int d[NMAX], real_d[NMAX];
    memset(d, 0x3f, sizeof(d));
    memset(Father, 0, sizeof(Father));
    myset.insert(make_pair(0, source));
    d[source] = real_d[source] = 0;
    while (!myset.empty()) {
        pair < int , int > P = *myset.begin();
        myset.erase(myset.begin());
        int nod = P.second;
        int ncost = P.first;
        if(d[nod] != ncost) continue;

        for (int i = 0; i < a[nod].size(); i++) {
            int next = a[nod][i];
            if (flux[nod][next] > 0) {
                int new_d = ncost + cost[nod][next] + old[nod] - old[next];
                if (new_d < d[next]) {
                    Father[next] = nod;
                    d[next] = new_d;
                    real_d[next] =  real_d[nod] + cost[nod][next];
                    myset.insert(make_pair(d[next], next));
                }
            }
        }
    }
    memcpy(old, real_d, sizeof(d));
    if (Father[destination]) return true;
    return false;
}

int maxflow(const int & source, const int & destination)
{
    int flow = 0, costflow = 0;
    for (; BF(source, destination);) {
        int flowmin = INF;
        int q = 0;

        for (int nod = destination; nod != source; nod = Father[nod])
        {
            flowmin = min(flowmin, flux[Father[nod]][nod]);
            q += cost[Father[nod]][nod];
        }
        for (int nod = destination; nod != source; nod = Father[nod]){
            flux[Father[nod]][nod] -= flowmin;
            flux[nod][Father[nod]] += flowmin;
        }

        costflow += flowmin * q;
    }
    return costflow;
}

int main()
{
    int source, destination;
    f >> n >> m >> source >> destination;
    for (int i = 0; i < m; i++) {
        int x, y, fl, c;
        f >> x >> y >> fl >> c;
        a[x].push_back(y);
        a[y].push_back(x);
        flux[x][y] = fl;
        cost[x][y] = c;
        cost[y][x] = -c;
    }
    bellman(source, destination);

    g << maxflow(source, destination);
    return 0;
}