Cod sursa(job #1754860)

Utilizator dominiciorgandaDominic Iorganda dominiciorganda Data 8 septembrie 2016 20:55:04
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define inf 1e6
using namespace std;
int k,i,x,m,j,g[360][360],h[360][360],cost[360][360],k1,i1,ct1,ok,h4[360],h1[360],h2[360],h3[360];
vector<int>f[360];
long long ct;
queue<int>q;
int bell()
{
    int k,i,m;
    for(k=1;k<=x;k++)
    {
        h1[k]=0;
        h2[k]=inf;
        h3[k]=0;
    }
    for(q.push(k1),h2[k1]=0,h3[k1]=1;!q.empty();)
    {
        k=q.front(); q.pop();
        m=h4[k];
        h3[k]=0;
        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]);
                }
            }
        }
    }
    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("%d%d%d%d",&x,&m,&k1,&i1);
    for(k=1;k<=m;k++)
    {
        scanf("%d%d%d%d",&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(k=1;k<=x;k++)
        h4[k]=f[k].size();
    for(ok=1,ct=0;ok;)
    {
        ok=0;
        ct+=bell();
    //    cout<<ct<<endl;
    }
    printf("%lld\n",ct);
    return 0;
}