Cod sursa(job #2003476)

Utilizator Horia14Horia Banciu Horia14 Data 22 iulie 2017 23:07:26
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.9 kb
#include<cstdio>
#include<queue>
#define MAX_N 350
#define oo 2000000000
using namespace std;

int n, m, S, D, k, result, costMin;
int C[MAX_N+1][MAX_N+1], F[MAX_N+1][MAX_N+1], Cost[MAX_N+1][MAX_N+1];
int dist[MAX_N+1], h[MAX_N+1], pos[MAX_N+1], T[MAX_N+1];
bool used[MAX_N+1];
queue<int>Q;

struct Node
{
    int val;
    Node* next;
};

Node* L[MAX_N+1];

inline int minim(int x, int y)
{
    if(x < y) return x;
    return y;
}

inline void insertBack(int x, int y)
{
    Node* elem = new Node;
    elem->val = y;
    elem->next = L[x];
    L[x] = elem;
}

void BellmanFord(int x)
{
    int i, node;
    Node* p;
    for(i=1; i<=n; i++)
        dist[i] = oo;
    dist[x] = 0;
    Q.push(x);
    used[x] = true;
    while(!Q.empty())
    {
        node = Q.front();
        Q.pop();
        used[node] = false;
        p = L[node];
        while(p != NULL)
        {
            if(F[node][p->val] < C[node][p->val] && dist[p->val] > dist[node] + Cost[node][p->val])
            {
                dist[p->val] = dist[node] + Cost[node][p->val];
                if(!used[p->val])
                {
                    Q.push(p->val);
                    used[p->val] = true;
                }
            }
            p = p->next;
        }
    }
    result = dist[D];
}

inline void Swap(int i, int j)
{
    int aux;
    aux = h[i];
    h[i] = h[j];
    h[j] = aux;
    aux = pos[h[i]];
    pos[h[i]] = pos[h[j]];
    pos[h[j]] = aux;
}

void heapDown(int i)
{
    int l, r;
    if(2*i > k) return;
    l = dist[h[2*i]];
    if(2*i+1 <= k)
        r = dist[h[2*i+1]];
    else r = l + 1;
    if(l < r)
    {
        if(dist[h[i]] <= l) return;
        Swap(i,2*i);
        heapDown(2*i);
    }
    else
    {
        if(dist[h[i]] <= r) return;
        Swap(i,2*i+1);
        heapDown(2*i+1);
    }
}

void heapUp(int i)
{
    if(dist[h[i/2]] <= dist[h[i]]) return;
    Swap(i,i/2);
    heapUp(i/2);
}

bool Dijkstra(int x)
{
    int i, node;
    Node* p;
    for(node = 1; node <= n; node++)
        if(dist[node] != oo)
        {
            p = L[node];
            while(p != NULL)
            {
                if(dist[p->val] != oo)
                    Cost[node][p->val] += dist[node] - dist[p->val];
                p = p->next;
            }
        }
    for(i=1; i<=n; i++)
    {
        dist[i] = oo; T[i] = 0;
        h[i] = pos[i] = i;
    }
    dist[x] = 0;
    Swap(1,S);
    k = n;
    while(k > 0&& dist[h[1]] != oo)
    {
        node = h[1];
        Swap(1,k);
        k--;
        heapDown(1);
        p = L[node];
        while(p != NULL)
        {
            if(F[node][p->val] < C[node][p->val] && dist[p->val] > dist[node] + Cost[node][p->val])
            {
                dist[p->val] = dist[node] + Cost[node][p->val];
                T[p->val] = node;
                heapUp(pos[p->val]);
            }
            p = p->next;
        }
    }
    if(dist[D] != oo) return 1;
    return 0;
}

int main()
{
    int i, x, y, cap, cost, flow, node;
    FILE *fin, *fout;
    fin = fopen("fmcm.in","r");
    fout = fopen("fmcm.out","w");
    fscanf(fin,"%d%d%d%d",&n,&m,&S,&D);
    for(i=1; i<=m; i++)
    {
        fscanf(fin,"%d%d%d%d",&x,&y,&cap,&cost);
        insertBack(x,y);
        insertBack(y,x);
        C[x][y] = cap;
        Cost[x][y] = cost;
        Cost[y][x] = -cost;
    }
    BellmanFord(S);
    while(Dijkstra(S))
    {
        flow = oo;
        for(node = D; node != S; node = T[node])
            flow = minim(flow,C[T[node]][node] - F[T[node]][node]);

        for(node = D; node != S; node = T[node])
        {
            F[T[node]][node] += flow;
            F[node][T[node]] -= flow;
        }
        result += dist[D];
        costMin += flow * result;
    }
    fprintf(fout,"%d\n",costMin);
    fclose(fin);
    fclose(fout);
    return 0;
}