Pagini recente » Cod sursa (job #3217510) | Cod sursa (job #2118610) | Cod sursa (job #3177382) | Cod sursa (job #1779066) | Cod sursa (job #701443)
Cod sursa(job #701443)
# include <fstream>
#include <cmath>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
unsigned m,n,a,b,c,i,flux[1005],viz[1005],fluxmax[1005];
struct flow{
unsigned x;
flow *urm;
}*v[1005];
void fluxul(int nod)
{
flow *p=v[nod];
while(p)
{
if(!flux[p->x])
{
if(nod!=1&&p->x!=n)
flux[p->x]=min(flux[nod],fluxmax[p->x]);
else
if(p->x!=n)
flux[p->x]=fluxmax[p->x];
else
{
int fluxbun=min(flux[nod],fluxmax[nod]);
flux[p->x]+=fluxbun;
fluxmax[nod]-=fluxbun;
}
}
else
if(p->x!=n)
flux[p->x]=max(flux[p->x],min(flux[nod],fluxmax[p->x]));
else
{
int fluxbun=min(flux[nod],fluxmax[nod]);
flux[p->x]+=fluxbun;
fluxmax[nod]-=fluxbun;
}
fluxul(p->x);
p=p->urm;
}
}
int main()
{
f>>n>>m;
for(i=1;i<=m;i++)
{
flow *p=new flow;
f>>a>>b>>c;
fluxmax[b]=c;
p->x=b;
p->urm=v[a];
v[a]=p;
}
fluxul(1);
g<<flux[n]<<'\n';
f.close();
g.close();
return 0;
}