Cod sursa(job #1754400)

Utilizator dominiciorgandaDominic Iorganda dominiciorganda Data 8 septembrie 2016 02:40:02
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int k,i,x,m,j,g[1005][1005],h1[1005][1005],ct,k1,i1,j1,m1;
vector<int>f[1005],h;
const long long inf=1e6;
bool bfs()
{
    int k,i,m;
    queue<int>q;
    h=vector<int>(x+1,0);
    for(q.push(1),h[1]=1;!q.empty();)
    {
        k=q.front(); q.pop();
        m=f[k].size();
        for(i=0;i<m;i++)
        {
            if(!h[f[k][i]]&&g[k][f[k][i]]>h1[k][f[k][i]])
            {
                h[f[k][i]]=k;
                q.push(f[k][i]);
                if(f[k][i]==x)
                    return 1;

            }
        }
    }
    return 0;
}
int main()
{
    freopen("maxflow.in","r",stdin);
    freopen("maxflow.out","w",stdout);
    scanf("%d%d",&x,&m);
    for(k=1;k<=m;k++)
    {
        scanf("%d%d%d",&i,&j,&ct);
        g[i][j]=ct;
        f[i].push_back(j);
        f[j].push_back(i);
    }
    for(;bfs();)
    {
        m=f[x].size();
        for(i=0;i<m;i++)
        {
            ct=inf;
            k=f[x][i];
            if(g[k][x]==h1[k][x]||!h[k])
                continue;
            h[x]=k;
    for(k=x;k!=1;k=h[k])
       ct=min(ct,(g[h[k]][k]-h1[h[k]][k]));
    if(!ct)
        continue;
    for(k=x;k!=1;k=h[k])
    {
            h1[k][h[k]]-=ct;
            h1[h[k]][k]+=ct;
    }
        k1+=ct;
        }
    }
    printf("%d\n",k1);
    return 0;

}