Cod sursa(job #1798375)

Utilizator iulianrotaruRotaru Gheorghe-Iulian iulianrotaru Data 5 noiembrie 2016 10:40:32
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
const int INF=(1LL<<31)-1;
int n,m,i,x,y,c,totalFlow;
int ant[1<<10],rez[1<<10][1<<10];
vector<int> V[1<<10];
bool bfs(int s,int d)
{
    bool viz[1<<10];
    memset(viz,0,sizeof viz);
    queue<int> q;
    viz[s]=1;
    q.push(s);
    ant[s]=s;
    while(!q.empty())
    {
        int nod=q.front();
        for(int i=0;i<V[nod].size();++i)
        {
            int x=V[nod][i];
            if(!viz[x]&&rez[nod][x]>0)
            {
                viz[x]=1;
                if(x==d) return 1;
                q.push(x);
                ant[x]=nod;
            }
        }
        q.pop();
    }
    return 0;
}
void solve()
{
    int s=1;
    int d=n;
    while(bfs(s,d))
    {
        for(int i=0;i<V[d].size();++i)
        {
            int nod=V[d][i];
            int maxFlow=INF;
            ant[d]=nod;
            for(int i=d;i!=ant[i];i=ant[i])
                maxFlow=min(maxFlow,rez[ant[i]][i]);
            totalFlow+=maxFlow;
            for(int i=d;i!=ant[i];i=ant[i])
            {
                rez[ant[i]][i]-=maxFlow;
                rez[i][ant[i]]+=maxFlow;
            }
        }
    }
    g<<totalFlow;
}
int main()
{
    f>>n>>m;
    for(i=1;i<=m;++i)
    {
        f>>x>>y>>c;
        V[x].push_back(y);
        V[y].push_back(x);
        rez[x][y]+=c;
    }
    solve();
    return 0;
}