Pagini recente » Cod sursa (job #2006186) | Cod sursa (job #2822883) | Cod sursa (job #1623975) | Cod sursa (job #2948866) | Cod sursa (job #936770)
Cod sursa(job #936770)
#include <fstream>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
#define LE 1000
int flux[LE][LE],cap[LE][LE];
int i,j,result,color,n,m,xx,yy,nodf,node;
bool okz=true;
int viz[LE],father[LE],mflow;
#define inf 1<<30
void dfs(int nod)
{
if (nod==n)
{
okz=true;
return;
}
if (viz[nod]==color||okz==true)
return;
viz[nod]=color;
for(int i=1; i<=n; ++i)
if (cap[nod][i]-flux[nod][i]>0)
{
if (okz==true) return;
father[i]=nod;
dfs(i);
}
}
int main()
{
f>>n>>m;
for(i=1; i<=m; ++i)
{
f>>xx>>yy;
f>>cap[xx][yy];
}
for (; okz==true;)
{
okz=false;
++color;
dfs(1);
nodf=node=n;
mflow=inf;
while (node!=1)
{
mflow=min(mflow,cap[father[node]][node]-flux[father[node]][node]);
node=father[node];
}
while (nodf!=1)
{
flux[father[nodf]][nodf]+=mflow;
flux[nodf][father[nodf]]-=mflow;
nodf=father[nodf];
}
result+=mflow;
}
g<<result<<'\n';
f.close();
g.close();
return 0;
}