Pagini recente » Cod sursa (job #2546762) | Cod sursa (job #2749682) | Cod sursa (job #1677989) | Cod sursa (job #1033287) | Cod sursa (job #794023)
Cod sursa(job #794023)
#include <fstream>
#define LE1 1065
#define LE 5050
#define inf 1<<30
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int father[LE],i,n,m,cost[LE1][LE1],a,b,C,flux[LE1][LE1],result,viz[LE],col,Fmax;
void bfs(int nod)
{
viz[nod]=true;
int i;
for(i=1; i<=n; ++i)
if (cost[nod][i]&&viz[i]==false&&flux[nod][i]<cost[nod][i])
{
father[i]=nod;
bfs(i);
}
}
int main()
{
f>>n>>m;
for(i=1; i<=m; ++i)
{
f>>a>>b>>C;
cost[a][b]=C;
}
while (true)
{
for(i=1; i<=n; ++i)
{
father[i]=0;
viz[i]=0;
}
bfs(1);
if (father[n]==0) break;
int Nod=n;
Fmax=inf;
while (father[Nod]!=0)
{
Fmax=min(cost[father[Nod]][Nod]-flux[father[Nod]][Nod],Fmax);
Nod=father[Nod];
}
Nod=n;
while (father[Nod]!=0)
{
flux[father[Nod]][Nod]+=Fmax;
Nod=father[Nod];
}
result+=Fmax;
}
g<<result<<'\n';
f.close();
g.close();
return 0;
}