Cod sursa(job #1373268)

Utilizator goalexboxerFMI Alexandru Ionascu goalexboxer Data 4 martie 2015 17:42:02
Problema Flux maxim de cost minim Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 3.06 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;

#define MAXSIZE 355
#define FIN "fmcm.in"
#define FOUT "fmcm.out"
#define INF 200000

int n,m;
vector<int> graph[MAXSIZE];
int capacity[MAXSIZE][MAXSIZE];
int cost[MAXSIZE][MAXSIZE];
int c[MAXSIZE];
int minCost;
int father[MAXSIZE];
int depth[MAXSIZE];
int source,destination;

int read()
{
    freopen(FIN,"r",stdin);
    freopen(FOUT,"w",stdout);

    scanf("%d %d %d %d \n", &n,&m, &source, &destination);
    int x,y,z,t;
    for(int i=1;i<=m;i++)
    {
        scanf("%d %d %d %d \n", &x,&y,&z,&t);
        graph[x].push_back(y);
        capacity[x][y] = z;
        cost[x][y] = t;
    }

    return 0;
}

int BFS()
{
    queue<int> q;
    vector<int>::iterator it;
    vector<int>::iterator it2;

    for(int i=1;i<=n;i++)
    {
        father[i] = 0, c[i] = 0, depth[i] = 0;
    }

    vector<int> levels[MAXSIZE];
    levels[0].push_back(source);

    q.push(source);
    while(!q.empty())
    {
        int nod = q.front();

        c[nod] = c[father[nod]] + cost[father[nod]][nod];

        if(nod == source)
            depth[nod] = 0;
        else depth[nod] = depth[father[nod]] + 1;

        q.pop();

        if(nod == destination)
        {

            int level = depth[father[nod]] - 1;

            int minim = INF;
            for(it2 = graph[level].begin(); it2 != graph[level].end(); it2++)
            {

                if(capacity[*it2][destination] > 0)
                {
                   // printf("le node %d de cost %d \n", *it2, c[*it2]);
                    if(c[*it2] + cost[*it2][destination] < minim)
                    {
                        minim = c[*it2] + cost[*it2][destination];
                        father[destination] = *it2;
                    }
                }

               // printf("%d \n", *it2);
            }


            //printf("am terminat nivelul %d \n", level);


            return 1;
        }
        else
        {

            for(it = graph[nod].begin();it!=graph[nod].end();it++)
            {

                if(father[*it] == 0 && capacity[nod][*it] > 0)
                {
                    q.push(*it);
                    father[*it] = nod;

                    levels[depth[nod] + 1].push_back(*it);


                   //printf("inserez la nivelul %d nodul %d \n", depth[nod] + 1, *it);
                }
            }
        }

    }
    return 0;
}

int solve()
{
    while(BFS() == 1)
    {
        //get min value
        int minn = capacity[father[destination]][destination];
        for(int i=destination;i!=source;i=father[i])
        {
            if(minn > capacity[father[i]][i])
                minn = capacity[father[i]][i];
        }
        //printf("\n path ");
        for(int i=destination;i!=source;i=father[i])
        {
            //printf("nodul %d cost %d  \n", i, c[i]);
            minCost += minn * cost[father[i]][i];
            capacity[father[i]][i] -= minn;
        }


    }

    return 0;
}

int write()
{
    printf("%d",minCost);

    return 0;
}

int main()
{
    read();
    solve();
    write();

    return 0;
}