Cod sursa(job #2615366)

Utilizator betybety bety bety Data 14 mai 2020 15:05:54
Problema Flux maxim Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("maxflow.in");
ofstream cout("maxflow.out");
const int inf=2e9+3;
const int lim=1005;
struct Op
{
    int src;
    int dest;
    int capp;
    int flow;
};
int pred[lim];
queue<int> q;
vector<Op> edge;
vector<int> vec[lim];
int n,m,x,y,c,ans,st,dr;
int main()
{
    cin>>n>>m;
    edge.push_back({0,0,0,0});
    for(int i=1;i<=m;++i)
    {
        cin>>x>>y>>c;
        edge.push_back({x,y,c,0});
        vec[x].push_back(i);
        vec[y].push_back(i);
    }
    st=1;dr=n;
    do
    {
        memset(pred,0,sizeof(pred));
        q.push(st);
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(auto t:vec[x])
            if(!pred[edge[t].dest] and edge[t].dest!=st and edge[t].capp>edge[t].flow)
            {
                pred[edge[t].dest]=t;
                q.push(edge[t].dest);
            }
        }
        if(pred[dr])
        {
            int df=inf;
            for(int t=pred[dr];t;t=pred[edge[t].src])
                df=min(df,edge[t].capp-edge[t].flow);
            for(int t=pred[dr];t;t=pred[edge[t].src])
                edge[t].flow+=df;
            ans+=df;
        }
    }while(pred[dr]);
    cout<<ans<<'\n';
    return 0;
}