Cod sursa(job #2003701)

Utilizator Horia14Horia Banciu Horia14 Data 23 iulie 2017 18:11:36
Problema Flux maxim de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.56 kb
#include<cstdio>
#include<vector>
#include<queue>
#define MAX_N 350
#define oo 0x3f3f3f3f
using namespace std;

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

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

void BellmanFord(int x)
{
    int i, node;
    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;
        for(it = G[node].begin(); it != G[node].end(); it++)
        {
            if(F[node][*it] < C[node][*it] && dist[*it] > dist[node] + Cost[node][*it])
            {
                dist[*it] = dist[node] + Cost[node][*it];
                if(!used[*it])
                {
                    Q.push(*it);
                    used[*it] = true;
                }
            }
        }
    }
    result = dist[D];
}

inline void Swap(int i, int j)
{
    int 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);
}

inline bool Dijkstra(int x)
{
    int i, node;
    for(node = 1; node <= n; node++)
        if(dist[node] != oo)
            for(it = G[node].begin(); it != G[node].end(); it++)
                if(dist[*it] != oo)
                    Cost[node][*it] += dist[node] - dist[*it];
    for(i=1; i<=n; i++)
    {
        dist[i] = oo; T[i] = 0;
        h[i] = pos[i] = i;
    }
    dist[x] = 0;
    Swap(1,x);
    k = n;
    while(k && dist[h[1]] != oo)
    {
        node = h[1];
        Swap(1,k);
        k--;
        heapDown(1);
        for(it = G[node].begin(); it != G[node].end(); it++)
        {
            if(F[node][*it] < C[node][*it] && dist[*it] > dist[node] + Cost[node][*it])
            {
                dist[*it] = dist[node] + Cost[node][*it];
                T[*it] = node;
                heapUp(pos[*it]);
            }
        }
    }
    return (dist[D] != oo);
}

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);
        G[x].push_back(y);
        G[y].push_back(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;
}