Cod sursa(job #2265874)

Utilizator slym777Darii Dan slym777 Data 21 octombrie 2018 20:30:40
Problema Flux maxim de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.9 kb
#include <iostream>
#include <bits/stdc++.h>
#define Nmax 360
#define pb push_back
#define inf 2000000

using namespace std;

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

vector <int> V[Nmax];
int C[Nmax][Nmax],GR[Nmax][Nmax];
int tata[Nmax], dist[Nmax];

int Drum(int n, int s, int d)
{
    bool flag = true;
    int flux, x;
    for (int i = 1; i <= n; i++)
    {
        tata[i] = 0;
        dist[i] = inf;
    }
    dist[s] = 0;
    tata[s] = s;
   // cout << dist[d] << endl;
    while (flag)
    {
        //cout << "a\n";
        flag = false;
        for (int i = 1; i <= n; i++)
            for (int j = 0 ; j < (int)V[i].size(); j++)
            if (GR[i][V[i][j]] > 0 && dist[i] + C[i][V[i][j]] < dist[V[i][j]])
            {
                //cout << V[i][j] << " = " << dist[i] << "+" << C[i][V[i][j]] << "c\n";
                dist[V[i][j]] = dist[i] + C[i][V[i][j]];
                tata[V[i][j]] = i;
                flag = true;
            }
    }
//cout << dist[d] << "b\n";
    if (dist[d] < inf/2)
    {
        //cout << "b1\n";
        x = d;
        flux = inf;
        while (x != s)
        {
            flux = min(GR[tata[x]][x],flux);
            x = tata[x];
        }
        x = n;
        while (x != s)
        {
            GR[tata[x]][x] -= flux;
            GR[x][tata[x]] += flux;
            x = tata[x];
        }
        return flux*dist[d];
    }
    //cout << "b2\n";
    return 0;
}


int main()
{
    int n,m,s,d,x,y,c,z,fluxmax = 0;
    fin >> n >> m >> s >> d;
    for (int i = 0; i < m; i++)
    {
        fin >> x >> y >> c >> z;
        GR[x][y] = c;
        C[x][y] = z;
        C[y][x] = -z;
        V[x].pb(y);
        V[y].pb(x);
    }
    x = 1;
    while (x != 0)
    {
       // cout << x << "\n";
        x = Drum(n,s,d);
        fluxmax += x;
    }
    fout << fluxmax;
    return 0;
}