Pagini recente » Cod sursa (job #2104825) | Cod sursa (job #1235936) | Cod sursa (job #1179641) | Cod sursa (job #596853) | Cod sursa (job #2615448)
#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<pair<int,bool> > 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,1});
vec[y].push_back({i,0});
}
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.first].dest] and edge[t.first].dest!=st and edge[t.first].capp>edge[t.first].flow)
{
pred[edge[t.first].dest]=t.first;
q.push(edge[t.first].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;
}