Cod sursa(job #1754807)

Utilizator dominiciorgandaDominic Iorganda dominiciorganda Data 8 septembrie 2016 19:03:16
Problema Flux maxim de cost minim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define inf 1e6
using namespace std;
long long k,i,x,m,j,ct,g[360][360],h[360][360],cost[360][360],k1,i1,ct1,ok;
vector<int>f[360],h1,h2,h3;
int bell()
{
    int k,i,m;
    h1=vector<int>(x+1,0);
    h2=vector<int>(x+1,inf);
    h3=vector<int>(x+1,0);
    queue<int>q;
    for(q.push(k1),h2[k1]=0,h3[k1]=1;!q.empty();)
    {
        k=q.front(); q.pop();
        m=f[k].size();
        for(i=0;i<m;i++)
        {
            if(g[k][f[k][i]]>h[k][f[k][i]]&&h2[k]+cost[k][f[k][i]]<h2[f[k][i]])
            {
                h2[f[k][i]]=h2[k]+cost[k][f[k][i]];
                h1[f[k][i]]=k;
                if(!h3[f[k][i]])
                {
                    h3[f[k][i]]=1;
                    q.push(f[k][i]);
                }
            }
        }
        h3[k]=0;
    }
    if(h2[i1]==inf)
        return 0;
    ok=1;
    for(k=i1,ct1=inf;k!=k1;k=h1[k])
        ct1=min(ct1,g[h1[k]][k]-h[h1[k]][k]);
    for(k=i1;k!=k1;k=h1[k])
    {
        h[h1[k]][k]+=ct1;
        h[k][h1[k]]-=ct1;
    }
    return ct1*h2[i1];

}
int main()
{
    freopen("fmcm.in","r",stdin);
    freopen("fmcm.out","w",stdout);
    scanf("%lld%lld%lld%lld",&x,&m,&k1,&i1);
    for(k=1;k<=m;k++)
    {
        scanf("%lld%lld%lld%lld",&i,&j,&ct,&ct1);
        f[i].push_back(j);
        f[j].push_back(i);
        g[i][j]=ct;
        cost[i][j]=ct1;
        cost[j][i]=-ct1;
    }
    for(ok=1,ct=0;ok;)
    {
        ok=0;
        ct+=bell();
    }
    printf("%lld\n",ct);
    return 0;
}