Cod sursa(job #1374287)

Utilizator Eugen01Vasilescu Eugen Eugen01 Data 5 martie 2015 03:03:35
Problema Flux maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.21 kb
#include<fstream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>

#define Nmax 355
#define inf 0x3f3f3f3f

using namespace std;

ifstream in("fmcm.in");
ofstream out("fmcm.out");

struct cmp
{
    bool operator() (const pair<int, int> &x, const pair<int, int> &y)
    {
        return x.first > y.first;
    }
};

int oldD[Nmax], newD[Nmax], D[Nmax];
priority_queue<pair<int, int>, vector<pair<int, int> >, cmp > PQ;
vector<int> G[Nmax];
int Capacity[Nmax][Nmax], Cost[Nmax][Nmax], parent[Nmax];

bool dijkstra(int source, int destination)
{
    bool inQueue[Nmax];

    memset(inQueue, false, sizeof(inQueue));
    memset(D, inf, sizeof(D));
    D[source] = 0;

    PQ.push(make_pair(0, source));
    while (!PQ.empty())
    {
        int node = PQ.top().second;
        int cost = PQ.top().first;
        PQ.pop();

        if (node == destination || D[node] != cost) continue;

        for (int i = 0; i < G[node].size(); i++)
        {
            int nextNode = G[node][i];
            int newCost = D[node] + oldD[node] - oldD[nextNode] + Cost[node][nextNode];

            if (Capacity[node][nextNode] && newCost < D[nextNode])
            {
                parent[nextNode] = node;
                D[nextNode] = newCost;

                newD[nextNode] = newD[node] + Cost[node][nextNode];
                PQ.push(make_pair(D[nextNode], nextNode));
            }
        }
    }
    memcpy(oldD, newD, sizeof(D));

    if (D[destination] != inf) return true;
    return false;
}

void bellmanFord(int source)
{
    queue<int> Q;
    bool inQueue[Nmax];

    memset(oldD, inf, sizeof(oldD));
    memset(inQueue, false, sizeof(inQueue));
    inQueue[source] = true;
    oldD[source] = 0;

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

        for (int i = 0; i < G[node].size(); i++)
        {
            int nextNode = G[node][i];
            int cost = Cost[node][nextNode];

            if (Capacity[node][nextNode] && oldD[node] + cost < oldD[nextNode])
            {
                D[nextNode] = oldD[node] + cost;
                if (!inQueue[nextNode])
                {
                    Q.push(nextNode);
                    inQueue[nextNode] = true;
                }
            }
        }
        inQueue[node] = false;
    }
}


int main()
{
    int N, M, source, destination;

    in >> N >> M >> source >> destination;

    for (int i = 1; i <= M; i++)
    {
        int x, y, c, z;
        in >> x >> y >> c >> z;

        G[x].push_back(y);
        G[y].push_back(x);

        Capacity[x][y] = c;
        Cost[x][y] = z;
        Cost[y][x] = -z;
    }

    bellmanFord(source);

    int totalCost = 0;
    while (dijkstra(source, destination))
    {
        int node = destination, flow = inf, cost = 0;

        while (node != source)
        {
            flow = min(flow, Capacity[parent[node]][node]);
            node = parent[node];
        }

        node = destination;

        while (node != source)
        {
            Capacity[parent[node]][node] -= flow;
            Capacity[node][parent[node]] += flow;

            cost += flow * Cost[parent[node]][node];
            node = parent[node];
        }

        totalCost += cost;
    }

    out << totalCost << "\n";
}