Cod sursa(job #1360019)

Utilizator goalexboxerFMI Alexandru Ionascu goalexboxer Data 25 februarie 2015 10:50:18
Problema Flux maxim de cost minim Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 2.74 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 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;

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

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

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

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

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

                if(*it == destination)
                {
                    int vertex = father[nod];
                    if(father[nod] == 0)
                        vertex = nod;
                    //printf("parcurg adiacentii lui %d \n", vertex);
                    int minim = INF;
                    for(vector<int>::iterator iit = graph[vertex].begin(); iit != graph[vertex].end(); iit++)
                    {
                        if(capacity[*iit][destination] > 0)
                        {
                            if(c[*iit] + cost[*iit][destination] < minim)
                            {
                                minim = c[*iit] + cost[*iit][destination];
                                father[destination] = *iit;
                            }
                        }
                    }
                    return 1;
                }

            }
        }

    }
    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;
}